scanf() Function in C Programming

scanf() function, how to take input and how to store them and use them.

In C programming scanf() function is a standard library function which is used to receive the values of variables from the keyboard.

Let’s take an example of scanf() function, in below example let supply the value of variables a and b from the keyboard.

Explanation

On the above example:

  • Both %d %d are format specifier, which indicates two int type input.
  • &a is address of variable a, which recieves first input.
  • &b is address of variable b, which recieves second input.

Must Remember

  • Within the double quotes, there should be only format specifications 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.
  • In the scanf() function, 3 things are very important i.e. format specifier, address operator, and variable.

Examples based on scanf() function.

Example 1

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

Output

Example 2

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

Output

Important points about scanf() function

  • Number of format specifiers and number of variable’s address must be same.
  • Order of format specifiers and data type of variable’s address must be same.

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)