Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).

Similar presentations


Presentation on theme: "Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard)."— Presentation transcript:

1 Lecture No: 16

2 The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard). The scanf() function read information for numbers and other datatypes from standard input (often console or command prompt). The scanf() function prototype is: scanf(“ format specifier”, &variable list);

3 Scanf() function cont…. In prototype of scanf() function, the format strings could be %f for input real values %d for input integer values %c for input char values etc… Following are some examples of usage of scanf() statement. scanf(“%d”, &a); scanf(“%d %f”, &i,&f);

4 As you can see, that the format for scanf() looks very much like that for printf(), the arguments on the left is a string that contain format specifier and variables names on the right side. The format specifiers for scanf() are similar to printf()function. %d for integer values %f or %e for real values %c for character %l for long int %lf for double type.

5 In pervious examples, you may notice that “&” operator before the variable name in scanf() statement is must. “&” is a pointer operator or it is also called Address operator or ampersand. The meaning or working of this operator is that it tells the memory address of variable where the input value will be store. Example: printf(“Enter value:”); scanf(“%d”, &a); Output: Enter value: 6 13062 13063 13064 13065 13066 13067 13068 a 6

6 Taking multiple values using scanf() function The scanf() function accept several variable at once. Means we can input several input values in single scanf staement. To demonstrate this, lets take the following programming example: void main(void) { int i; char c; float f; printf(“Enter values for i, c and f variable); scanf(“%d %c %f”, &i,&c,&f); printf(“value of i is %d, value of c is %c and value of f is %f”, i,c,f); getch(); }

7 The scanf() function accept several variable at once. Means we can input several input values in single scanf statement. To demonstrate this, lets take the following programming example: void main(void) { int i; char c; float f; printf(“Enter values for i, c and f variable); scanf(“%d %c %f”, &i,&c,&f); printf(“\nvalue of i is %d, value of c is %c and value of f is %f”, i,c,f); getch(); } Taking multiple values using scanf() function OUTPUT Enter values for i c and f variable: 2 B 36.34 Value of i is 2, value for c is B and value for f is 36.34

8 In pervious example, now the question will arise here that how does scanf() knows when we’ve finished typing one value and start another? Answers is that, As we type our three input values (suppose 2,B, 36.34) we separate them by spaces. The scanf() matches each space we type with the corresponding space b/w the format specifier in scanf(). If we have tried to separate the values with another character – (dashed or comma) this would not have matched the space in format strings/ specifier. This process is shown in next slide.

9 Taking multiple values using scanf() function 13062 13063 13064 13065 13066 13067 13068 13061 13060 13059 Scanf(“%d %c %f”, &i,&c,&f); i c f 2 B 36.34

10

11 Keywords Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called “Reserved words”. There are only 32 keywords available in C. The list of keywords in C are shown in next slide.

12 auto double if static case breakelse int struct enum long switch char extern near while typedefconstfloat register union continuefarreturn unsigned default forshortvoid do goto signed Keywords


Download ppt "Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard)."

Similar presentations


Ads by Google