What is scanf in C Programming

Scanf in C

Scanf in C

scanf() is a predefined standard library function, it is used to take input from the keyboard. scanf() function is defined in stdio.h header file.

In C programming input is taken by scanf() function, the input values stored in variables and later these variable's value used in the program.

scanf() Syntax

scanf("%d %d", &a, &b);


On the above example:

  • Both %d %d are format specifiers, which indicates two int-type inputs.
  • &a is the address of variable a, which receives the first input.
  • &b is the address of variable b, which receives the second input.

scanf() Function Rules

  • Within the double quotes, there should be only format specifiers like %c, %d, %f, etc.
  • Variable names must be preceded by the address of operator &.
  • To use scanf() function must include stdio.h header file in the program.
  • The number of format specifiers and the number of variable addresses must be the same.
  • Order of format specifiers and data type of variable‚Äôs address must be the same.

scanf() Function Examples

Example 1

Write a program that takes two numbers as input and prints their sum as output.


void main()


    int a,b,sum;

    printf("Enter Two Number \n");

    scanf("%d %d", &a, &b);



    printf("Sum of both numbers is %d", sum);




Enter Two Number

12 44

Sum of both numbers is 56


Example 2

Take inputs employee name, age, and salary and print employee details as a story.


void main()


    char emp;

    int age, sal;

    printf("Enter Employee Name \n");

    scanf("%c", &emp);

    printf("\n Enter Employee Age \n");

    scanf("%d", &age);

    printf("\n Enter Employee Salary \n");

    scanf("%d", &sal);

    printf("\n Story is ------ \n");

    printf("There was an employee Mr. %c \n", emp);

    printf("He was %d year old \n", age);

    printf("He was earning $%d in a month\n", sal);

    printf("He was very happy in his life");




Enter Employee Name


Enter Employee Age


Enter Employee Salary


Story is ------

There was an employee Mr. A

He was 28 year old

He was earning $2500 in a month

He was very happy in his life

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

LifeStyle & Fun

© 2024 GDATAMART.COM (All Rights Reserved)