Assignment Operators Statement Equivalent Statement aa2 a 2

  • Slides: 8
Download presentation
Assignment Operators = += -= *= /= %= Statement Equivalent Statement a=a+2; a +=

Assignment Operators = += -= *= /= %= Statement Equivalent Statement a=a+2; a += 2 ; a=a-3; a -= 3 ; a=a*2; a *= 2 ; a=a/4; a /= 4 ; a=a%2; a %= 2 ; b=b+(c+2); b += c + 2 ; d=d*(e-5); d *= e - 5 ;

int number = 10, sum = 0; What is the output? int i; printf("enter

int number = 10, sum = 0; What is the output? int i; printf("enter int: "); scanf("%d", &i); switch (i) { case 1: printf("Exam 2 =>Nov 18n"); printf("1: sum =%d number=%dn", sum, number); break; case 2: number +=3; printf("2: sum =%d number=%dn", sum, number); case 3: sum = number++; printf("3: sum =%d number=%dn", sum, number); case 4: sum = --number; printf("4: sum =%d number=%dn", sum, number); case 5: printf("fuzzballn"); break; default: sum = number; printf("D: sum =%d number=%dn", sum, number); } printf("Final: sum =%d number=%dn", sum, number);

Problems with Reading Characters p p When getting characters, whether using scanf( ) or

Problems with Reading Characters p p When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character. What will the user actually type? The character he/she wants to enter, followed by pressing ENTER. So, the user is actually entering two characters, his/her response and the newline character. Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.

Reading Chars #include <stdio. h> int main ( ) { char my. Char, newline

Reading Chars #include <stdio. h> int main ( ) { char my. Char, newline ; printf (“Enter a character: “) ; my. Char = getchar( ) ; newline = getchar( ) ; /* could also usescanf(“%c”, &newline) ; */ printf (“The value of |%c| is |%c|. n”, my. Char, newline ) ; return 0 ; } User’s input is a Lets use this as an Enter symbol What happens when user hits Enter key on the keyboard? n

Reading Chars #include <stdio. h> int main ( ) { char my. Char='o', newline

Reading Chars #include <stdio. h> int main ( ) { char my. Char='o', newline ; printf(“Enter e to exitn") ; while(my. Char!=‘e') { printf("Enter a character: ") ; my. Char = getchar( ); newline = getchar( ); printf ("The value of |%c| is |%c|. n", my. Char, newline ) ; } printf(“See You Later!") ; return 0; }

Additional Concerns with Garbage in stdin p p p When we were reading integers

Additional Concerns with Garbage in stdin p p p When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer. That is because scanf( ) was looking for the next integer and ignored the newline (whitespace). If we use scanf (“%d”, &num); to get an integer, the newline is still stuck in the input stream. If the next item we want to get is a character, whether we use scanf( ) or getchar( ), we will get the newline. We have to take this into account and remove it.

switch in a event controlled loop p Create a menu that continually prompts the

switch in a event controlled loop p Create a menu that continually prompts the user to enter 'H' to print a greeting in English, ‘A’ to print a greeting in Hawaiin, 'N' to print a greeting in Hindi, and S to print a greeting in Hebrew. The menu should also include an option 'E' which exits the program. Menu should look something like this: Please enter: H -> To print greeting in English A -> To print greeting in Hawaiian N -> To print greeting in Hindi S -> To print greeting in Hebrew E -> To exit 'H', it prints "Hello"; 'A' it prints "Aloha"; 'N' it prints "Namasthe"; ‘S’ it prints "Shalom". 'E', the program should print "Adios" before it exits the program.

getchar( ) Example Using EOF #include <stdio. h> int main () { int grade,

getchar( ) Example Using EOF #include <stdio. h> int main () { int grade, a. Count, b. Count, c. Count, d. Count, f. Count ; a. Count = b. Count = c. Count = d. Count = f. Count = 0 ; while ( (grade = getchar( ) ) != EOF ) { switch ( grade ) { case ‘A’: a. Count++; break ; case ‘B’: b. Count++; break ; case ‘C’ : c. Count++; break ; case ‘D’: d. Count++; break ; case ‘F’: f. Count++; break ; default : break ; } } return 0 ; }