New version page

Purdue CS 15900 - Chapter 3 Structure of a C Program

Documents in this Course
Load more
Upgrade to remove ads

This preview shows page 1-2-3 out of 10 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

Chapter 3Structure of a C ProgramCS 159 - C ProgrammingExpressions▪ Operator – syntactical token that requires an action to be taken (i.e. + - / * %)▪ Operand – object which receives an operator's action (i.e. a variable or a constant)Expression – a sequence of at least one operand, and zero or more operators that reduces to a single value.Program Standards: One space must be placed between all operators and operands (except for unary operators). For example:y = 5 * (x / z) – g++;Primary Expressions▪ Literal Constant5, 25.7, 'a', "string"▪ Identifier such as a variable or a functionx, itemPrice, size▪ Parenthetical Expression(3 * 5 + x / 2)Unary ExpressionsExpressionDescription+xValue of the operand is unchanged.-xValue of the operand isnegated.++xVariable is incremented by 1 before the rest of the statement is executed--xVariable is decremented by 1 before the rest of the statement is executedfunct()Value returned from a function call (with any passed parameters)sizeof xSize of the variable in bytes is given(type) xExpression is cast to the specified data type Operator OperandUnary ExpressionsExpressionDescriptionx++Variable is incremented by 1 after the rest of the statement is executedx--Variable is decremented by 1 after the rest of the statement is executedOperatorOperandPrefix/Postfix Expressions▪ Prefix/Postfix Increment/Decrement must use a variableas its operand▪ Prefix expressions take place before the entire statement is evaluated ++x (equivalent to x = x + 1)--x (equivalent to x = x – 1)▪ Postfix Expressions take place after the entire statement is evaluated x++ (equivalent to x = x + 1)x-- (equivalent to x = x – 1)Prefix/Postfix ExpressionsIf a variable is updated multiple times in the same expression the result may be unpredictable and should not be done.int x = 1;int y;y = 3 + x-- * 2;int x = 1;x = x-- + ++x;Side effect – in addition to the value returned by the expression, a variable is modified or another external interaction occurs.side effectPrefix/Postfix ExpressionsWhat is the difference between x++ and ++x?▪ There is no difference if it is on a line by itself▪ But there can be a difference if there is a side effectint x = 1;printf("%d ",x);printf("%d ",x++);printf("%d ",++x);printf("%d ",x);int x = 1;printf("%d ",x);printf("%d ",++x);printf("%d ",x++);printf("%d ",x);Binary ExpressionsOperator OperandOperandExpressionDescriptionx + yBoth operands are added togetherx – yRight operand is subtracted from the left operandx * yBoth operands are multiplied togetherx / yLeft operand is divided by the right operandx % yLeft operand is divided by the right operand and the remainder is given#include <stdio.h>int main(void){int num1 = 5;int num2 = 3;float num3 = 5.1;float num4 = 3.2;printf("Integral calculations\n");printf("%d + %d = %d\n", num1, num2, num1 + num2);printf("%d - %d = %d\n", num1, num2, num1 - num2);printf("%d * %d = %d\n", num1, num2, num1 * num2);printf("%d / %d = %d\n", num1, num2, num1 / num2);printf("%d %% %d = %d\n", num1, num2, num1 % num2);printf("Floating-point calculations\n");printf("%f + %f = %f\n", num3, num4, num3 + num4);printf("%f - %f = %f\n", num3, num4, num3 - num4);printf("%f * %f = %f\n", num3, num4, num3 * num4);printf("%f / %f = %f\n", num3, num4, num3 / num4);return 0;}Integral calculations5 + 3 = 85 - 3 = 25 * 3 = 155 / 3 = 15 % 3 = 2Floating-point calculations5.100000 + 3.200000 = 8.3000005.100000 - 3.200000 = 1.9000005.100000 * 3.200000 = 16.3200005.100000 / 3.200000 = 1.593750Operator PrecedenceLeft-to-RightRight-to-LeftAssociativity – expressions of the same precedence are done in the specified sequential order.3 + 2 – 1 + 38 % 4 * 2 / 3a = b = c = 0a += b *= 5These are usually considered bad programming practice.Operator PrecedencePrecedence – specifies the priority order in which various operators are executed in an expressions.8 % 3 + 2 / 38 % (3 + 2) / 3“In C expressions, you can assume that *, /, and % come before + and -. Put parentheses around everything else.”– Steve Oualline, C Elements of StyleOperator PrecedencePriorityOperatorDescriptionAssociativity16variableVariableleft-to-rightconstantLiteral constant()Parenthetical expressionfunct()Functional call++ --Postfix increment/decrement15++ --Prefix increment/decrementright-to-leftsizeofSize ofobject in bytes+ -Plus/minus14(type)Type castingright-to-left13* / %Multiplication/division/modulusleft-to-right12+ -Addition/subtractionleft-to-right2=Assignmentright-to-left*= /= %=Addition/subtraction assignment+= -=Multiplication/division/modulus assignmentStatementsStatement – the smallest standalone element of a programming language which expresses some action to be performed.StatementDescriptionExampleExpressionThe expression is evaluated but no result is stored. expression;NullNo statementis executed.;AssignmentThe expression is evaluated and the result is stored in the variable on the leftvariable = expression;CompoundBlockA group of statements and can be substituted in place of any single statement.{statement;statement;...}Statements▪ Statements are usually terminated by a semicolon• Do not use a semicolon after a compound block because that would add a null statement• Do not use a semicolon after a #define because that is a preprocessing statementwould be the same as{amount = 4;count = 3;};#define TAX_RATE .07;salestax = TAX_RATE * price;salestax = .07; * price;#define TAX_RATE .07;salestax = TAX_RATE * price;Assignment Statements▪ The left operand must always be a single variable▪ The variable memory location is updated only after the entire expression is evaluatedVariable Expression;Assignment Statementsint x;int y = 2;x = 3 * 4;y = -x + 20;x = x + 1; x = y + x;y = y % 3;x = y++;x + 1 = y;x y----- -----? ? 212 212 813 821 821 22 3errorCompound Assignment StatementsCompound StatementEquivalent Statementx += expressionx =x + expressionx -= expressionx = x – expressionx *= expressionx = x * expressionx /= expressionx = x / expressionx %= expressionx = x % expressionCompound StatementEquivalent Statementx += expressionx =x + (expression)x -= expressionx = x – (expression)x *= expressionx = x * (expression)x /= expressionx = x / (expression)x %= expressionx = x % (expression)Implicit Type Conversions1. Each item in an expression is automatically elevated to the rank of the higher ranking data type in the order it is encountered by the rules of


View Full Document
Download Chapter 3 Structure of a C Program
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 3 Structure of a C Program and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 3 Structure of a C Program 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?