C Programming - Lecture V
Dr. Sorin Draghici
Wayne State University
Summary
- The 'comma' operator
- More assignment operators
- if-else, switch
- continue
- break
- 'getchar', 'putchar'; buffered I/O
- Functions
The 'comma' operator
The 'comma' operator is a sequence point i.e. it guarantees that the expression on its left will be evaluated before the expression on its right.
The value of an expression using a 'comma' operator is the value of the right-hand member
Examples:
More assignment operators
x = x + 1 ;
x += 1 ;
int y = 3 ;
y = y/2 ;
y /= 2 ;
One can use any combination: arithmetic operator - assignment operator
More examples: +=, -=, *=, /=, %=
If-then-else
if ( condition )
else
If the condition is true, statement1 will be executed; if the condition is false, statement2 will be executed.
'condition' is a (logical) expression (but remember that any non-zero value is true)
If-then-else
The 'else' branch can miss:
if ( condition )
The 'if' statements can be nested:
if ( condition1 )
else
If-then-else
x = 1 ; y = 20 ;
if ( x < 3 )
else
printf("three\n") ;
Exam question: What does this fragment produce as output ?
Dangling 'else': the 'else' belongs always to the last if.
If-then-else
More exam questions:
x = 1 ; y = 20 ;
if ( x < 3 )
else
- printf("two\n" ) ;
- printf("three\n") ;
printf("four\n");
Exam question: What does this fragment produce as output ?
Switch
if ( x == 1 )
else if ( x == 2 )
else if ( x == 3 )
else
- printf("greater than three\n");
Switch
switch( x )
- {
- case 1 : printf("one\n") ;
- case 2 : printf("two\n" ) ;
- case 3 : printf("three\n") ;
- default: printf("greater than three\n");
- }
…will produce:
one
two
three
greater than three
'continue'
for( i = 0 ; i < 10 ; i ++ )
- {
- sum += i ;
- if ( sum > 5 )
- printf("%d ", sum ) ;
- }
printf("\nThe sum is: %d\n", sum ) ;
'continue' causes the rest of the current iteration to be skipped; the next iteration will be started
'break'
for( i = 0 ; i < 10 ; i ++ )
- {
- sum += i ;
- if ( sum > 5 )
- printf("%d ", sum ) ;
- }
printf("\nThe sum is: %d\n", sum ) ;
'break' determines the exit from the loop (innermost if there are nested loops)
Two functions: 'putchar' and 'getchar'
A function is a self-contained unit of program code designed to accomplish a particular task (subroutines, procedures).
A function receives a number of parameters and returns a value
You have to declare the function (so that other functions can use it) and
You have to define the function (to say what and how the function does its job)
'getchar' and 'putchar'
int getchar ( void ) ;
int putchar ( char ) ;
These are the declarations: we now know the parameters and the return values.
Example:
int c ;
c = getchar() ;
putchar( c ) ;
while( (c=getchar()) != 'q' )
putchar( c ) ;
Buffered I/O
Buffered input:
outside world -> buffer -> your program
Buffered output:
your program -> buffer -> outside world
The program gains access to the content of the buffer only when \n is encountered.
More info in chap. 8 and the Unix part
Summary
The 'comma' operator
More assignment operators
Branching and jumps:
if-else, switch
continue
break
'getchar', 'putchar'; buffered I/O
Functions
Reading
Chap. 7 (pag. 206-248)
Chap. 8 (pag. 250-276) - we know this stuff from the Unix part of the course!!!
Chap. 9 (pag. 278-293) - it's not all of it!
As usual: Pay attention to the review questions!