Hello everyone, I hope you must have done the practical test of our previous programs. Remember practical knowledge is utmost important in learning c language.
Anyways till we have covered the basic use of printf() function by which we can print values on the screen. Today we will learn how to take values from the user.
Note: Read previous article to know more about printf() function: First C Program – Hello World
scanf() in C
scanf() is used to take data from the user. Till now we have wrote programs in which we declared variables with some values. But in practice we need those programs which are general enough to make computations.
So with the help of scanf() function now we will make a general program for multiplication of two numbers. In this program we will ask the user to enter the values.
printf("Enter two values to do multiplication");
printf("Your answer is %d",c);
Now lets try to understand this program.
1. First two instructions are same like our previous programs.
2. In the third instruction we are declaring three variables of integer type.
3. In the fourth instruction we are printing the statement using printf() function.
4. In the fifth instruction we are taking input from the user through scanf() function.
In this scanf() function we have done two things.
a. We have given the format specifier %d to instruct the compiler that we want to input integer value.
b. We have used ampersand (&) which is also called “address of operator”. By using this we instruct the compiler we want to store that input in that variable (a and b).
Why do we use ampersand operator (&)?
As I have said already it is a “address of operator”. By using this operator we specify the address of variable to the compiler.
A bit confusion..? Ok, checkout the example below.
Suppose we have used &a. Now C compiler will receive the input and go to the address of a (which can be anything like 7635). After that it will store that value on that particular address. That’s it.
Lets write another program which is slightly complicated i.e. program to calculate simple interest.
C Program to Calculate Simple Interest
In this program I am assuming that you must know the formula and working of simple interest in mathematics. So I will not explain that formula to you.
/*Program to calculate simple interest
TheCrazyProgrammer date 21/12/14*/
int p,n; //Here p is principle amount and n is number of years
float r,si; //Here r is rate of interest and si is simple interest
printf("Enter the values of p,n and r");
printf("Simple interest is %f",si);
Lets try to understand this program step by step.
1. First two statements are comments.
Comments in C
Comments are generally used to increase the readability of program. At present we are making very small programs. But when we develop big programs then the program has to go through a long process of testing and debugging. Comments are not the part of program code and are not read by compiler.
It is very important to write comments in programs. So that other programmers can also read and understand your program easily. Writing comments is also a good programming practice. Start writing comments in the programs from the beginning itself.
C allows two types of comments
a. Single line comment: // first type of comment
b. Multiline comment: /* second type of comment*/
Single line comment is used to write comments in one line only. Multiline comment is used to write comments in multiple lines. All things that comes in between /* and */ is considered as comment. We can use anyone according to requirement.
2. After that next three instructions are same which includes C pre-processor directives, main() function, declaration of integer variables.
3. In the fourth instruction we have declared float variable r and si. Because rate of interest can be a floating point number. And to stay on safe side we also declared si variable as float. As the answer may come in floating point.
4. After that using printf() function we print a message to instruct the user to insert the values of p, n and r.
5. Using scanf() we are taking input from the user. Checkout we have used %f format specifier for r variable. We have declared r as float variable. So we have to use %f format specifier to print as well as receive values in r variable.
6. Now in the next statement we have calculated the simple interest using the formula.
7. In the last we have print the answer using printf() function. Notice we have used %f format specifier there. As si is also a float variable.
So these are the basic use of printf() and scanf() functions. These are one of the most used functions in C language. So you can estimate the importance of them. Now you can make 100s of programs by using these two functions.
Try making these programs yourself (take values from user)
1. Make a program to add two numbers.
2. Make a program which will convert distance in km to meter.