The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Input / Output in C

C library provides inbuilt functions for taking user input and displaying the program output. These functions are declared in stdio.h header file. We will illustrate the use of these functions using sample codes.

printf ( ) function
printf is a function which is used to display integers, characters, strings, etc. on the output screen.
Consider the following C statements :

int x = 20; /* a value 20 is assigned to variable x */
printf("Value of x : %d\n", x); /* prints the value of x onto screen */

Above printf ( ) function would print " Value of x : 20 " on the screen.
We have used %d format specifier in the printf statement to specify that the value to be printed is an integer. Basically, %d is replaced by 20 in the statement. Similarly, for displaying a character, we have to use %c format specifier. Following table shows the list of format specifiers :

Format SpecifierData Type Specified
%d , %iInteger
%uUnsigned Integer
%ldLong Signed Integer
%luLong Unsigned Integer
%e, %E, %f, %g, %GFloating Point Number
%x , %XHexadecimal
%pAddress Format

We will illustrate all the data types mentioned in the second column later in the tutorial. In the above printf statement, we have used an escape sequence i.e \n which is a newline character. The use of \n causes the next characters to be printed in the next line. Following table shows the list of escape sequences :

Escape SequenceCharacter Represented
\tTab space
\rCarriage Return

Following program illustrates the use of format specifiers and escape sequences in printf function.

 * This program illustrates the use of format specifiers and escape sequences.

int main() {
    int x = 17;                  /* integer */
    float y = 9.324;             /* float */
    char ch = 'R';               /* character */
    double z = 3.127845;         /* double */
    char str[10] = "csegeek";    /* string */
    printf("Integer : %d\tFloat : %f\n", x , y);
    printf("Character : %c\tDouble : %lf\n", ch , z);
    printf("String : %s\n", str);
    printf("\nOctal Equivalent of %d is %o\n", x, x);
    printf("Hexadecimal Equivalent of %d is %x\n", x, x);
    return 0;

scanf ( ) function
scanf is a function which is used to display integers, characters, strings, etc. from the keyboard. The use of format specifiers are applicable to scanf ( ) function as well. While taking input in a variable, we must use ampersand ( & ) character before the variable as shown in the program below :

 * This program illustrates the use of scanf function to input data.

int main() {
    char ch; /* declare a variable 'ch' of type character */
    int val; /* declare a variable 'val' of type integer */
    char str[30]; /* declare a string to store max 30 characters */
    printf("Enter a character : ");
    scanf("%c", &ch);                /* input a character */
    printf("Enter an integer : ");
    scanf("%d", &val);               /* input an integer */
    printf("Enter a string : ");
    scanf("%s", &str);               /* input a string */
    printf("\nYou have entered \n");
    printf("Character : %c\nInteger : %d\n", ch, val);
    printf("String : %s\n", str);
    return 0;
Run this program in your system to take input at run-time  

You can skip rest of the stuffs discussed below for now and come back to it after learning about strings but it is recommended to go through these concepts once as it won't take much time.

String Input / Output
A string is an array of characters. We will discuss more about strings later in the tutorial. Right now we just focus on reading a string from keyboard and displaying it on screen. We have seen how to input a string using scanf function but it has a drawback. It stops reading characters once a space character is encountered i.e if you try to input a string "cse geek", scanf will read only "cse". The C library provides the following functions to input a string and display it.

gets(str)Reads a string from keyboard.
Dangerous to use this because it doesn't check for length of the input string.
This may lead to buffer overflow ( Suppose the string is declared as
char str [ 10 ] and user enters a string of length > 10 ).
fgets(str, 10, stdin)Safer form of gets as it contains a length parameter.
puts(str)Displays the string onto the screen.

Following program illustrates the use of string input / output functions :

 * This program illustrates the use of fgets and puts functions

int main() {
    char str[20]; /* str can store a string of max 20 characters */
    printf("Enter a string : ");
    fgets(str, 20, stdin); /* stdin means input from keyboard */
    printf("\nYou have entered : ");
    puts(str); /* display the string */
    return 0;
Run this program in your system to take input at run-time  

sscanf ( ) , sprintf ( ) and snprintf ( )
sprintf doesn't display the output on the screen, rather it stores the output in a buffer. snprintf is same as sprintf but it has an additional argument n which specifies the number of characters to be stored in the buffer. sscanf reads data from a buffer instead of keyboard. Following program illustrates the use of these functions.

 * This program illustrates the use of sscanf and sprintf functions

int main() {
    char str1[50], str2[50], str3[50];
    int x = 20;
    sprintf(str1, "Value of x = %d", x);
    snprintf(str2, 8, "Value of x = %d", x);
    puts(str1); // prints "Value of x = 20"
    puts(str2); // prints "Value of" i.e first 8 characters only
    sscanf(str2 ,"%s", str3); // reads data from str2 and stores in str3
    puts(str3); // prints "Value" ( sscanf stops reading when it sees a space )
    return 0;

Similarly, we have fscanf and fprintf functions to read from and write to a file respectively. We will discuss these later in the module on file handling.

Back | Next