Reading Input from the Terminal

In C, the scanf() function is used to read input from the terminal. The function is included in the stdio.h header file and has the following syntax:

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

The format string is a string of characters that specifies the type of input that is expected, and the variables are memory locations where the input values will be stored.

Some common format specifiers that can be used in the format string are:

  • %d: Used for reading integers.
  • %f: Used for reading floating-point numbers.
  • %c: Used for reading characters.
  • %s: Used for reading strings.


#include <stdio.h>

int main() {
   int age;
   float salary;
   char name[30];
   printf("Enter your name: ");
   scanf("%s", name);
   printf("Enter your age: ");
   scanf("%d", &age);
   printf("Enter your salary: ");
   scanf("%f", &salary);
   printf("Name: %s\n", name);
   printf("Age: %d\n", age);
   printf("Salary: %.2f\n", salary);
   return 0;

When the program is run, it will prompt the user to enter their name, age, and salary. The scanf() function will read the input values and store them in the respective variables. The printf() function is then used to display the input values on the screen.

The scanf() function is an important tool for user input and interaction in a program. It can be used to read values from the user, process the input, and generate appropriate output.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial