In C language, variables are assigned values directly and then we can use them in different types of expressions and get the results. But the initial values given to the variables are fixed and does not changes. Sometimes the user wants to enter the input values themselves and check the results for different input values.
int a , b, c;
a = 10;
b = 20;
c = a + b;
In the example above, the variable a and b has fixed values – 10 and 20, which decide the result of the program. If the user wants to test the program with a different set of input values, he or she needs to change the source code every time.
Reading Input with scanf()
Fortunately C programming language has a builtin function called scanf() which helps read input value during the run-time of the program. There is not need to change the input values from source code every time user wants a different input value.
The function scanf() is a C standard library function. It means that the C library supports different features of C programming language using a list of header files. To use any library function such as scanf() you need to include the header file containing that library function.
The scanf() is part of header file – stdio.h
Syntax for scanf()
The syntax for scanf() is given below.
First we declared a variable called number of type integer. Then we used scanf statement. This statement has two important elements.
- format specifier
- address of operator
The format specifier tells what data type to read or write. It is very important to specifier what you are reading during the execution of a program, otherwise the C program will not recognize the input values. These are the common format specifiers.
- %d – for integer
- %f – for float
- %c – for character type
- %s – for string type
Address of Operator (&)
You may come across this operator time during this tutorial. This operator (&) is called the “address of” operator and it tells the memory location of the variable, so that, we can read input value and store at that address.
In the following program, we will read quantity of two fruits that a customer purchased and then calculate the total amount payable.
float unit_price_apple, unit_price_orange;
int total_no_of_apples, total_no_of_oranges;
float total_cost = 0.0;
/* let's assign initial values to unit prices */
unit_price_apple = 2.50;
unit_price_orange = 1.50;
/* Read the quantity of apples and oranges purchased by the customer */
printf("Enter total no of apples purchased:");
printf("Enter total no of oranges purchased:");
/* Compute total costs */
total_cost = (total_no_of_apples * unit_price_apple) + (total_no_of_oranges * unit_price_orange);
The input and output of the example program is given below.
Reading String or Char Inputs – getchar() and gets()
Sometimes it is easier to use use builtin functions to read string or character type variable values. The C language has two popular function in the library for this purpose.
The getchar() is part of stdio.h header file and helps reading a single character. The following example show the usage and syntax for getchar() library function.
Output – getchar()
When the program is executed, user must input a grade for a student. The getchar() function will receive the input grade which is a single character and assign it to variable grade. The grade is printed as an output to the program.
The gets() function reads a string of characters. It is also part of stdio.h header file and a standard C library function. It is very useful in many string related programs.
A string is nothing but an array of characters with a terminating null character (\0). The gets() function copies the string of characters until a newline or end-of-file (EOF) is reached. To help you understand the syntax and usage of this function check the example program below.
Output – gets()
The output of the above program is given below.
Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.
Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.