You are on page 1of 2

scanf function in c language

The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use. The simplest application of scanf looks like this: scanf("%d", &b); The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b. The scanf function uses the same placeholders as printf: int uses %d float uses %f char uses %c character strings (discussed later) use %s You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it. In general, it is best to use scanf as shown here -- to read a single value from the keyboard. Use multiple calls to scanf to read multiple values. In any real program, you will use the gets or fgets functions instead to read text a line at a time. Then you will "parse" the line to

read its values. The reason that you do that is so you can detect errors in the input and handle them as you see fit. The printf and scanf functions will take a bit of practice to be completely understood, but once mastered they are extremely useful
Read more: scanf function in c language http://www.friendsmania.net/forum/notescomputer-science-notes/99046.htm#ixzz1T3rIgBY2

You might also like