In C, the input from a standard input device, such as a keyboard, is taken by using the function scanf. In scanf, you have to specify both the variables in which you can take input, and the format specifier, such as %d for the integer.
Program
#include
main()
{
int i,j,k;
scanf("%d%d",&i,&j); // statement A
k = i + j;
printf("sum of two numbers is %d \n",k);
}
Input 3 4
Output: sum of two numbers is 7
Explanation
1. Statement A indicates the scanf statement that is used for taking input. In scanf you have to specify a list of addresses of variables (&i, &j) which can take input. Before specifying the list of variables you have to include a list of format specifiers that indicate the format of the input. For example, for the integer data type the format specifier is %d.
2. In scanf, you have to specify the address of the variable, such as &i. The address is the memory location where a variable is stored. The reason you must specify the address will be discussed later.
3. The number of format specifiers must be the same as the number of variables.
Point to Remember
In scanf, you have to specify the address of a variable, such as &i, &j, and a list of format specifiers.
No comments:
Post a Comment