For more free Urdu Tutorials click here: http://bit.ly/ZcRlXc
Like us on facebook: http://on.fb.me/13Z3CqS
Follow us on twitter: http://bit.ly/13Z3LKP
This video is about C programming Input with Scanf in Urdu. In this Urdu Tutorials Series I teach you how to program in C programming 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 and for simple programs it is good enough and easy-to-use.
The simplest application of scanf looks like this:
1
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.