C - Input Output operation using scanf and printf functions

C Programming


In this tutorial we will learn to handle input output operations in C programming language using the scanf and printf function.

In the previous tutorial we learned to handle single character input output operation using the getchar() and putchar() functions.

scanf function

We use the scanf() function to get input data from keyboard.

Format of scanf() function.

scanf("control string", &variable1, &variable2, ...);

The control string holds the format of the data being received. variable1, variable2, ... are the names of the variables that will hold the input value.

The & ampersand symbol is the address operator specifying the address of the variable where the input value will be stored.

Lets explore some code to see scanf in action.

In the following example we will take two integer numbers as input from the keyboard and then print the sum.

#include <stdio.h>
int main(void)
  int a, b, sum;
  printf("Enter two integer numbers:\n");
  scanf("%d %d", &a, &b);
  sum = a + b;
  printf("Sum: %d\n", sum);
  return 0;


Enter two integer numbers:
Sum: 30

Format code for scanf function

Format Code Description
%c Read a character
%d Read a decimal integer
%ld Read a long integer
%hd Read a short integer
%e Read a floating point value
%f Read a floating point value
%lf Read a double number
%Lf Read a long double number
%g Read a floating point value
%h Read a short integer
%i Read a decimal, hexadecimal or octal integer
%o Read an octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a hexadecimal integer
%[..] Read a string of word(s)

printf function

We use the printf() function to print output.

The format of printf function to print output string.


In the following example we will print "Hello World" string using the printf function.

#include <stdio.h>
int main(void)
  printf("Hello World");
  return 0;

Format of printf function to output result using format code.

printf("some-output-text format-code", variable1, variable2, ...);

In the following example we will take an integer, double and a character value as input from the user and will output the entered data.

#include <stdio.h>
int main(void)
  //declare variables
  char ch;
  int i;
  double d;

  //take input from user
  printf("Enter an integer, a character and a double value: ");
  scanf("%d %c %lf", &i, &ch, &d);

  printf("Entered data\n");
  printf("Integer: %d\n", i);
  printf("Character: %c\n", ch);
  printf("Double: %lf\n", d);
  return 0;


Enter an integer, a character and a double value: 10 a 12.34
Entered data
Integer: 10
Character: a
Double: 12.340000

Format code for printf function

Format Code Description
%c Print a single character
%d Print a decimal integer
%ld Print a long integer
%hd Print a short integer
%e Print a floating point value in exponent form
%f Print a floating point value without exponent
%lf Print a double number
%Lf Print a long double number
%g Print a floating point value either e-type or f-type depending on condition.
%i Print a signed decimal integer
%o Print an octal integer, without leading zero
%s Print a string
%x Print a hexadecimal integer, without leading 0x

Recently Updated