To learn about the reading or receiving inputs from users in the c program we will take an example program and then understand it.

Let's look at the below-given program. if we assumed the values of p, n, and r to be 1000, 3, and 8.5. then every time we run the program we would get the same value for simple interest. if we want to calculate simple interest for some other set of values we are required to make the relevant change in the program, and again compile and execute it. Thus the program needs to be more general to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able to make changes in the program Hence it is a good practice to create a program that is general enough to work for any set of values.

To make the program general the program itself should ask the user to supply the values of p, n, and r through the keyboard during execution. this can be achieved using a function called scanf(). this function is a counterpart of the printf() function. printf() outputs the values to the screen whereas scanf() receives them from the keyboard. this is illustrated in the program shown below.

/*Calculate the simple interest */



int p, n;

float r,si;

printf("Enter values of p, n, r");

scanf("%d %d %f", &p,&n,&r);

si = p * n * r / 100;



The first printf() outputs the message ' Enter values of p, n, r ' on the screen. here we have not used any expression in printf() which means that using the expression in printf() is optional.

Note that the ampersand (&) before the variables in the scanf() function is a must. & is an 'Address of' operator. it gives the location number used by the variable in memory. when we say &a, we are telling scanf() at which memory location should it store the value supplied by the user from the keyboard.

Note that a blank, a tab, or a new line must separate the values supplied to scanf(). note that a blank is created using a spacebar, a tab using the Tab key, and a new line using the Enter key. this is shown below.

Example - The three values are separated by blank

1000 5 15.5

Example - The three values are separated by a tab.

1000 5 15.5

Example - The three values are separated by newline




Let's take another example that receives an integer input from the user.



int num;

printf("Enter a number");


printf("Now I am letting you on a secret...");

printf("You have just entered the number %d", num);