Wednesday, July 17, 2013

scanf() as Input Function in C

In C, the most commonly used Input Function is scanf(). This function takes the input from the standard input i.e., keyboard and sets the values taken to some variable. This scanf() function takes any type of data based on the format specifiers and then assigns the value to the variable of that particular data type when specified as arguments.

To know the prototype of the scanf() function, you can refer here.

Using scanf() function, we can take different types of data and also we can take values to any number of variables using a single scanf() statement.

In scanf() function also, we use the format specifiers to know the type of data, and assign the taken value to the variable, which follows as arguments.

Example:
scanf("%d",&a);
In the above example, if you observe it, we have used the format specifier and passed an argument &a. Here & is the address-of operator which we will be discussing later.

As of now, remember that it is the address of the variable a, we are passing this address to take the value into that address.

What happens if we don't place & before the value??? To know click here.