Signup/Sign In

C Input and Output

Input means to provide the program with some data to be used in the program and Output means to display data on the screen or write the data to a printer or a file.

The C programming language provides standard library functions to read any given input and to display data on the console.

Before moving forward with input and output in C language, check these topics to understand the concept better :

The functions used for standard input and output are present in the stdio.h header file. Hence to use the functions we need to include the stdio.h header file in our program, as shown below.

#include <stdio.h>

Following are the functions used for standard input and output:

  1. printf() function - Show Output

  2. scanf() function - Take Input

  3. getchar() and putchar() function

  4. gets() and puts() function

In C Language, computer monitor, printer, etc. output devices are treated as files and the same process is followed to write output to these devices as would have been followed to write the output to a file.

printf() function - Show Output

The printf() function is the most used function in the C language. This function is defined in the stdio.h header file and is used to show output on the console (standard output).

This function is used to print a simple text sentence or value of any variable which can be of int, char, float, or any other datatype.

printf() Example - Print a statement

Let's print a simple sentence using the printf() function.

#include <stdio.h>

int main() {
   // using printf()
   printf("Welcome to Studytonight");

   return 0;

Welcome to Studytonight

Run Code →

We have seen many code examples like above, up until now in this tutorial.

To understand the complete code and structure of a basic C language program, check Hello World Program in C.

printf() Example - Print Integer

We can use the printf() function to print integer value coming from variable using the %d format specifier.

For example,

#include <stdio.h>

int main() {
   int x = 10;
   // using printf()
   printf("Value of x is: %d", x);

   return 0;

Value of x is: 10

Run Code →

In the program, above we have used the %d specifier, to specify the type of the value that will be added there. The format specifiers %d and %i are used for integer value.

printf() Example - Print Character

The %c format specifier is used to print character variable value using the printf() function.

#include <stdio.h>

int main() {
   // using printf()
   char gender = 'M';
   printf("John's Gender is: %c", gender);
   return 0;

John's Gender is: M

Run Code →

Inside the printf() function we have used a single quotation, it will work fine because we have used double quotation outside. We can also use backslash character to escape the single quote like this \'

printf() Example - Print Float and Double

In the code example below, we have used the printf() function to print values of a float and double variable.

For float value we use the %f format specifier and for double value we use the %lf format specifier.

#include <stdio.h>
int main() {
   // using printf() 
   float num1 = 15.50;
   double num2 = 15556522.0978678;
   printf("Value of num1 is: %f \n", num1);
   printf("Value of num2 is: %lf", num2);

   return 0;

Value of num1 is: 15.500000
Value of num2 is: 15556522.097868

Run Code →

We have used the \n Escape sequence which is used for newline at the end of the first printf() statement so that the next printf() statement output is shown in the next line.

printf() Example - Print multiple outputs

We can use a single printf() function to display values of multiple variables.

#include <stdio.h>

int main() {
   // using printf() for multiple outputs 
   int day = 20;
   int month = 11;
   int year = 2021;
   printf("The date is: %d-%d-%d", day, month, year);

   return 0;

The date is: 20-11-2021

Run Code →

As you can see in the code example above, we can do formatting as well while printing values of multiple variables using the printf() function.

Format Specifiers

To print values of different datatypes using the printf() statement, we need to use format specifiers, like we have used in the code examples above.

Also, when we take input from user using the scanf() function, then also, we have to specify what type of input to expect from the user using these format specifiers.

Here is a list of all the format specifiers.

Datatype Format Specifier
int %d, %i
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double %Lf

scanf() function - Take Input

When we want to take input from the user, we use the scanf() function. When we take input from the user, we store the input value into a variable.

The scanf() function can be used to take any datatype input from user, all we have to take care is that the variable in which we store the value has the same datatype.

Let's see a few code examples.

scanf() Example - Take Integer value input

If we have to take an integer value input from the user, we have to define an integer variable and then use the scanf() function.

#include <stdio.h>
int main() {
   // using scanf() 
   int user_input;
   printf("Please enter a number: ");
   scanf("%d", &user_input);
   printf("You entered: %d", user_input);

   return 0;

Run Code →

Please enter a number: 7
You entered: 7

In the above code example, we have used %d format specifier to inform the scanf() function that user input will be of type integer.

And we have also used & symbol before the name of the variable, because &user_input refers to the address of the user_input variable where the user input value will be stored.

scanf() Example - Take Float value input

Just like integer value, we can take input for any different datatype. Let's see an example for float type value.

#include <stdio.h>
int main() {
   // using scanf() 
   float user_input;
   printf("Please enter a decimal number: ");
   scanf("%f", &user_input);
   printf("You entered: %f", user_input);

   return 0;

Please enter a decimal number: 7.007
You entered: 7.007

We have used the %f format specifier and defined a float type variable. Try doing the same for taking a double type value as user input. The format specifier for double is %lf.

scanf() Example - Take Character value input

Let's see how we can take a simple character input from user.

#include <stdio.h>
int main() {
   // using scanf() 
   char gender;
   printf("Please enter your gender (M, F or O): ");
   scanf("%c", &gender);
   printf("Your gender: %c", gender);

   return 0;

Please enter your gender (M, F or O): M
Your gender: M

scanf() Example - Take Multiple Inputs

#include <stdio.h>
int main() {
   // using scanf() for multiple inputs
   char gender;
   int age;

   printf("Enter your age and then gender(M, F or O): ");
   scanf("%d %c", &age, &gender);
   printf("You entered: %d and %c", age, gender);

   return 0;

Enter your age and then gender(M, F or O): 32 M
You entered: 32 and M

*Some Extra Information

The printf() function returns the number of characters printed by it, and scanf() returns the number of characters read by it.

int i = printf("studytonight");
printf("Value of i is: %d", i);

studytonightValue of i is: 12

In this program printf("studytonight"); will return 12 as result, which will be stored in the variable i, because the word studytonight has 12 characters.

The first printf() statement will print the statement studytonight on the output too.

getchar() & putchar() functions

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only a single character at a time.

You can use this method in a loop in case you want to read more than one character.

The putchar() function displays the character passed to it on the screen and returns the same character. This function too displays only a single character at a time.

In case you want to display more than one character, use putchar() method in a loop.

#include <stdio.h>

void main( )
    int c;
    printf("Enter a character");
        Take a character as input and 
        store it in variable c
    c = getchar();
        display the character stored 
        in variable c 

Enter a character: Studytonight

When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered.

gets() & puts() functions

The gets() function reads a line from stdin(standard input) into the buffer pointed to by str pointer, until either a terminating newline or EOF (end of file) occurs.

The puts() function writes the string str and a trailing newline to stdout.

str is the pointer to an array of chars where the C string is stored. (Ignore if you are not able to understand this now.)

#include <stdio.h>

void main()
    /* character array of length 100 */
    char str[100];
    printf("Enter a string: ");
    return 0;

Enter a string: Studytonight

When you will compile the above code, it will ask you to enter a string. When you will enter the string, it will display the value you have entered.

Difference between scanf() and gets()

The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as a character too.

If you enter a name as Study Tonight using scanf() it will only read and store Study and will leave the part after space. But gets() function will read it completely.


In this tutorial we learned about Input and Output in the C language. With many code examples, we understood how to use printf() function and scanf() function to show output to user and take input from the user.

We also learned about a few other functions used for showing output and taking user input.