14.1 Learn the layout of a C program
14.2 Learn the Assignment statements in C
14.3 Learn some of the difference between C and FORTRAN
SOME TYPICAL ASSIGNMENT STATEMENTS
EQUIVALENT STATEMENTS USING ARITHMETIC OPERATORS
EXAMPLES USING PRINTF( ) LIBRARY FUNCTION
VALID STATEMENTS IN C
LOGICAL EXPRESSIONS IN C
mark = mark/7 ;
price = price + tax ;
price = price * tax + price ;
bonus = bonus - 7 ;
total = total * 5 ;
month = month % 12 + 1 ;
mark /= 7 ;
price += tax ;
price *= tax + price ;
bonus -= 7 ;
total *= 5 ;
month %= 12 + 1 ;
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);
a = b+c;
count = scanf ("%d", &n);
scanf ("%d", &n);
( These always end with semicolons)
;
{
statement
{
statement temp = x ;
x = y ;
y = temp ;
}
statement
}
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 "); }
BIT OPERATORS
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) size of | |
* / % | |
+ - | |
<< >> | |
< <= > >= | |
= = != | |
& | |
^ | |
| | |
&& | |
|| | |
?: | |
= += -= *= /= %= &= ^= |= <<= >>= | |
, |
On to the next lecture
Go back to lecture menu