Professional Documents
Culture Documents
CH 3 Expressions
CH 3 Expressions
Expressions
Expression Type
3 Constant
0x04A Constant
myAge Variable
x Variable
3+87 Constants combined via operator
x + 48.09 Constant and variable combined via operator
(5.0/9.0) * (tFar -32.0) Constant and variable combined via operator
3.1 Constants
The C has two types of constants.
• Literal Constants
• Symbolic Constants
3.2 Variables
A variable is a named data storage location in computer’s Random Access Memory (RAM).
• To have a variable in C programming language, first it must be declared.
• The general form for variable declaration is
typename varname;
• Eg. In the Listing 3.2, a variable named x of type int has been declared by int x;.
• The keyword int defines the type of to be integer
– that is, the x can contain an whole number (not fractional numbers)
– 4 bytes are allocated for int type variables
– and therefore the values that can be assigned to x is limited to the range -2,147,483,648 to -
2,147,483,647
• The name of the variable x is then used in the program to access the variable. Examples
– x=1; the x is assigned the value 1.
– x=x+6; the 6 is added to x and the result (7) is then assigned back to x.
– y=x; the y is assigned the value of x
• C’s numeric variable types are summarized in Table 3.1
• Integer variables hold values that have no fractional part (i.e. whole numbers). Integer numbers come in
two flavors:
– signed integer variables can hold positive or negative values, whereas
– unsigned integer variables can hold only positive values (and zero).
• Floating-point variables hold values that have a fractional part (i.e. real Numbers)
2
Listing 3.2: Declaring and using a variable
columns
columns
# include < stdio .h > co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ int x ; /* declaration : int type variable x */ co
int y ; /* declaration : int type variable y */
columns
columns co
columns
columns co
x = 1;
columns
columns /* write : Assign 1 to x */ co
columns
columns
x = x + 6; /* Assigne x +6 to x */ co
columns
columns
y = x; /* read : from variable x and write to y */ co
columns
columns co
columns
columns
return 0; co
}
columns
columns co
scanf("%d", &x);
where,
– &x is the address of x
– "%d" indicates that the user input characters must be formatted as an integer (specified by the letter
d) and then store in x.
– The letter d is one of the format specifiers in C
printf()
• The following example shows how to use printf() to write an integer stored in int type variable x on
the Terminal.
printf("%d", x);
where,
– printf() prints characters of the text string given within " and ".
– However, in place of the format specifier \%d, it prints the value of the variable given after the ,
(that is value of x) after formating it as indicated by the format specifier (that is as an integer)
– The letter d is one of the format specifiers in C
– Format specifier should match with the type of the variable (x)
– The list of format specifies is listed in Table 3.3.
– How to print some of other variable types are given in the Listing 3.3.
the
Variable Type
The variable type selected in your program must match with the
• type of information that you intended to store (integer , fractional numbers, character, string, ..)
• expected range of values (maximum and minimum) that might store in the variable.
3.4 Operators
• An operator is a symbol that instructs C to perform some operation, or action on one or more operands.
• An operand is something that an operator acts on.
• In C, all operands are expressions.
• C operators fall into several categories.
– The assignment operator
– Mathematical operators
– Relational operators
– Logical operators
4
Table 3.3: C’s format specifiers
Briefly: ++i adds one to the stored value of i and ”returns” the new, incremented value to the surrounding
expression; i++ adds one to i but returns the prior, unincremented value.
5
Binary Mathematical Operators
• Usage of main operators +, -, *, /, %. must be clear!
• See the Listing 3.4 for exemplary usage of % operator
• x = 4 + 5 *3
– if 4 + 5 evaluated first then y becomes 27.
– if 5 * 3 evaluated first then y becomes 19.
• See Table 3.5 for complete list of operator precedence.
• If an expression contains more than one operator with the same precedence level, the operators are
performed from left to right. E.g. y=12 % 5 * 2;, x = 4 + 5 *3
• C uses parentheses to modify the evaluation order. A sub-expression enclosed in parentheses is evaluated
first, without regard to operator precedence. E.g. x = (4 + 5) *3
• Multiple and nested parentheses are possible. Evaluation proceeds from the innermost parentheses on-
wards. E.g. y = 25 - (2 * (10 + (8/2)));
• Do use parentheses to make the order of expression evaluation clear.
6
Table 3.4: C Operators
Operator Symbol Action Examples
Assignment = Left side expression is evaluated and re- x=y;
sulting value is assigned to right side vari-
able
Increment ++ Increments the operand by one ++x, x++
Decrement -- Decrements the operand by one --x, x--
Addition + Adds two operands x+y
Subtraction - Subtracts the left operand from the right x-y
operand
Less than < Is left operand less than right operand? x<y
Less than or <= Is left operand less than or equal to right x<=y
equal to operand?
Logical AND && Logical AND between operands (expres- exp1 && exp2
sions)
7
Bitwise Logical | Operands are logical OR in bitwise x | y
OR
Bitwise Logical ^ Operands are logical XOR in bitwise x ^ y
XOR
Bitwise Logical ~ Operand is logical NOT in bitwise ^x
Complement
8
Listing 3.3: Use of scanf() and printf() with some variable types
columns
columns
/* scanf () and printf () with some variable types co
*/
columns
columns co
columns
columns
# include < stdio .h > co
columns
columns co
int main ( void )
columns
columns co
columns
columns
{ co
unsigned char mGender ;
columns
columns co
columns
columns
int mAge ; co
float mMarksGeo , mMarksMth ;
columns
columns co
columns
columns
double mWeight , mHeight ; co
columns
columns co
columns
columns
printf ( " What is your gender ( M / F ) : " ) ; co
columns
columns
scanf ( " % c " ,& mGender ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your age ( in years ) : " ) ; co
scanf ( " % d " ,& mAge ) ;
columns
columns co
columns
columns co
columns
columns
printf ( " \ nWhat is your marks for Mathematics (0 -100) : " ) ; co
columns
columns
scanf ( " % f " ,& mMarksMth ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your marks for Geography (0 -100) : " ) ; co
columns
columns
scanf ( " % f " ,& mMarksGeo ) ; co
columns
columns co
columns
columns
printf ( " \ nWhat is your Weight ( in kg ) : " ) ; co
columns
columns
scanf ( " % lf " ,& mWeight ) ; co
columns
columns co
columns
columns
printf ( " \ ntWhat is your Height ( in m ) : " ) ; co
columns
columns
scanf ( " % lf " ,& mHeight ) ; co
columns
columns co
columns
columns
printf ( " \n - - - - - - - - - - - - - " ) ; co
columns
columns
printf ( " \n - Your Info -" ) ; co
columns
columns
printf ( " \n - - - - - - - - - - - - - " ) ; co
columns
columns co
columns
columns
printf ( " \ n Gender = % c \ t Age : % d " , mGender , mAge ) ; co
printf ( " \ n Marks for Geography = % f and for Mathamatics = % f " ,
columns
columns co
columns
columns
mMarksGeo , mMarksMth ) ; co
columns
columns
printf ( " \ n Avarage Marks = % f " , ( mMarksGeo + mMarksMth ) /2.0) ; co
columns
columns
printf ( " \ n Weight = % lf , Height = % lf " , mWeight , mHeight ) ; co
columns
columns
printf ( " \ n Ratio Weight / Height = % lf " , mWeight / mHeight ) ; co
columns
columns co
columns
columns co
return 0;
columns
columns co
columns
columns
} co
9
Listing 3.4: Demonstrating the modulus operator
10