Some Typical Assignment Statements
mark = mark/7 ; price = price + tax ; price = price * tax + price ; bonus = bonus - 7 ; total = total * 5 ; month = month % 12 + 1 ;
Equivalent Statements Using
Arithmetic Operators
mark /= 7 ; price += tax ; price *= tax + price ; bonus -= 7 ; total *= 5 ; month %= 12 + 1 ;
Examples Using printf( ) Library
Function
printf("Hello, World \n"); printf("%d %f %lf",integer,real,long_integer ); printf(" %c %s ", character, string ); printf(" %p ", pointer); printf(" %o ", octa_notation); printf(" %x ",hexa_notation); printf(" %u",unsigned_variable); printf(" %s is %d years old ",name,age); printf(" \t abcdefg \n\t hijklmn \n\t ... "); printf(" He got %f \% in his final exam !!!",grade);
VALID STATEMENTS IN C
a = b+c; count = scanf ("%d", &n); scanf ("%d", &n);
( These always end with semicolons
)
;
{ statement { statement temp = x ; x = y ; y = temp ; } statement }
LOGICAL EXPRESSIONS IN C
Example :
strcmp("b", "a") returns {1} (true because b > a) strcmp("b", "b") returns {0} (false because b cannot be big & small at the same time)
General Form of if ... else
Statement
if ( expression ) statement_1 ; else statement_2;
General Form of if statement
( without else statement )
if ( expression ) statement ;
or ( with more than one statement
)
if ( expression ) { statement_1; statement_2; . . . statement_n; }
Examples of Nested if Statements
Example #1 :
if ( grade >= 85 ) if ( final > 90 ) printf( "Outstanding student !"); else printf( "Excellent work !" );
Example #2 :
if( cost > sales ) if( cost/2 > 100*sales ) printf( "You are in big trouble !!!"); else printf( "You seem to be in trouble. ");
Example #3 :
if( house_value > 500000 ) if( number_cars > 3 ) printf( "You are running into debt !!!"); else printf( "You are rich." );
Example #4 :
if( house_value > 500000 ) { if( number_cars > 3 ) printf( "You are running into debt !!!"); } else printf( "Buy new cars !!!" );
General Form of if...else if...else
Statements
if (expression_1) statement_1; else if (expression_2) statement_2; else if (expression_3) statement_3; . . . else if ( expression_n ) statement_n; else statement_k;
Example Use of if...else if...else
Statement
/* Function to print the letter grade */ void print_letter_grade(float grade) { if ( grade < 50 ) printf(" F "); else if ( grade < 55 ) printf(" C- "); else if ( grade < 60 ) printf(" C "); else if ( grade < 65 ) printf(" C+ "); else if ( grade < 70 ) printf(" B- "); else if ( grade < 75 ) printf(" B "); else if ( grade < 80 ) printf(" B+ "); else if ( grade < 85 ) printf(" A- "); else printf(" A "); }
Explanation | |
bitwise logical AND | |
bitwise logical OR | |
bitwise exclusive OR | |
left shift | |
right shift | |
one's complement |
Full set of C operators in order
of decreasing precedence
Operator | |
( ) [ ] -> | |
! + - ++ -- * & (cast type) sizeof | |
* / % | |
+ - | |
<< >> | |
< <= > >= | |
= = != | |
& | |
^ | |
| | |
&& | |
|| | |
?: | |
= += -= *= /= %= &= ^= |= <<= >>= | |
, |
Go back to lecture menu
Go back to main page
Copyright © 1996 McGill University. All rights reserved.