Thursday, September 29, 2011

Common Input-Output Mistakes and Concepts in C

1.       printf(“sanjay”+2);   //prints njay only.
2.       Always use fflush(stdin) after any scanf statement.
3.       Always use & operator in scanf statement.
4.       We cannot use %d for inputting values other than int in scanf(for eg. long, float).
5.       Global variables initialize to 0, local to some garbage value. Global ‘char’ also initializes to 0.
6.       int/int is int. float/int is float. Int/float is float. (‘/’ represents division operator).
7.       scanf(“%c%c”,&ch1,&ch2);    //keyboard input must be ‘AB’ and not ‘A B’.
scanf(“%c %c”,&ch1,&ch2);  //keyboard input must be ‘A B’.
scanf(“%c/%c”,&ch1,&ch2);  //keyboard input must be ‘A/B’.
8.       printf returns the no. of bytes printed by it.
9.       scanf returns the no. of variables passed to it.
10.   sprintf(str, “%d %d”,x,y); // where str is a string.
sprintf does not prints on screen like printf, but it stores the formatted output in str.
Similarly sscanf takes the formatted input from str rather than from the keyboard.

No comments:

Post a Comment