1 / 12

Introduction to computer programming

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

olinda
Download Presentation

Introduction to computer programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to computer programming 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 6 a 13068

  6. Taking multiple values using scanf() function • The scanf() function accept several variable at once. • Means we can input several input values in single scanfstaement. • To demonstrate this, lets take the following programming example: void main(void) { inti; 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. Taking multiple values using scanf() function • The scanf() function accept several variable at once. • Means we can input several input values in single scanfstatement. • To demonstrate this, lets take the following programming example: void main(void) { inti; 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(); } 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 Scanf(“%d %c %f”, &i,&c,&f); 13059 13060 13061 2 13062 i 13063 13064 B 13065 c 13066 13067 13068 36.34 f

  10. KEYWORDS

  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. Keywords auto double if static case break else intstructenum long switch char extern near while typedef const float register union continue far return unsigned default for short void do goto signed

More Related