C Programming Input/Output

C Input/Output

There are a number of ways of getting input from user and giving output to user in C language. Some of them are briefly discussed here. They will be discussed in detail later apart from some other functions being used for the I/O purposes.

scanf() and printf() Functions

Most of the C programmers prefer to use scanf() function while taking input from user and prefer to use printf() function while giving output. The syntax of scanf() is as below.

scanf (“format specifiers”,&var1,&var2,…);

where scanf is a predefined function and format specifiers specify the type of values to input and store in the variables mentioned. The commonly used format specifiers are as below.

%d          integer
%f           float
%c          character
%s          string
%lf          double
%ld         long integer

It is important to note that that format specifiers are preceded by a % sign and the variables are preceded by & sign as mentioned in the example given below.

Similarly printf() function has the following syntax.

printf (“format specifiers”,var1,var2,…);

printf() follows almost the same syntax as that of scanf() function but the variables are not preceded by the & sign. The following example demonstrates the use of simple input/output in C language.

C language

The above C program takes two integer values from user as input and display the sum of the given integer values as output. scanf() is used at lines 7 and 8 to accept two integer values from user and store them in variables ‘a’ and ‘b’ respectively. We will discuss the variables in detail in a next article.

 At the moment you should know that variables are used to store the values given by the user or to store the result of a calculation.

The values given by user and the data type of variables must be same. One variable can store a single value at any given time. In the example the user will give two integer values. The first value will be stored in variable ‘a’ and the second value will be stored in variable ‘b’.

Once both the values are given by the user, the sum of these values are stored in another variable ‘c’ (line 10). In order to display the value of variable ‘c’, printf() function is used at line 11. If we execute the above program then we will get an output as below.


Having said earlier, we have a number of function used in C language to take input from user and displaying output.

Although programmers normally use scanf() and prinf() functions for these purposes but some other functions may be used in times when scanf() and prinf() functions do not satisfy the needs. The other functions used for I/O will be discussed in later tutorials.