printf scanf in C

The printf() and scanf() functions are used for input and output in C language.These functions are declared and related macros are defined in "stdio.h" which is a header file. We have to include ""stdio.h" file as shown in below C program to make use of these printf() and scanf() library functions.

C printf() function:

  • printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
  • We use printf() function with %d format specifier to display the value of an integer variable.
  • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
  • To generate a newline,we use “\n” in C printf() statement.

Note: C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

Example program for C printf() function:

#include <stdio.h>

int main()
char ch = 'A';
char str[20] = "";
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;


You can see the output with the same data which are placed within the double quotes of printf statement in the program except

  • %d got replaced by value of an integer variable (no),
  • %c got replaced by value of a character variable (ch),
  • %f got replaced by value of a float variable (flt),
  • %lf got replaced by value of a double variable (dbl),
  • %s got replaced by value of a string variable (str),
  • %o got replaced by a octal value corresponding to integer variable (no),
  • %x got replaced by a hexadecimal value corresponding to integer variable

C scanf() function:

  • scanf() function is used to read character, string, numeric data from keyboard
  • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given number.

#include <conio.h>
void main(){
int number;
printf("enter a number:");
printf("cube of number is:%d ",number*number*number);


enter a number:5

cube of number is:125

The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.

Program to print sum of 2 numbers

Let's see a simple example of input and output in C language that prints addition of 2 numbers.

#include <conio.h>
void main(){  
int x=0,y=0,result=0;  

printf("enter first number:");  
printf("enter second number:");  

printf("sum of 2 numbers:%d ",result);  



enter first number:9
enter second number:9
sum of 2 numbers:18