What if you could build applications integrating SQL database, freshdesk, salesforce, twitter, email, payment and sms gateways and many other cloud services without writing a single line of code?
Yes, it is possible. Explore the power of Azure Logic Apps with the newly introduced course on Udemy.
Access the course for free now. Offer expires soon.

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 Specifier Data Type Specified
%d , %i Integer
%u Unsigned Integer
%ld Long Signed Integer
%lu Long Unsigned Integer
%c Character
%e, %E, %f, %g, %G Floating Point Number
%lf Double
%o Octal
%x , %X Hexadecimal
%s String
%p Address 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 Sequence Character Represented
\n Newline
\t Tab space
\b Backspace
\r Carriage 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;

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.

Function Explanation
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;

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.

About    Contact    Sitemap    Terms    Privacy