How to Use scanf() Function in C Programming Language

scanf() Function in C Programming Language

Introduction :

In programming, user input is a very important task to create dynamic and interactive applications such as calculator, game etc. In C programming language, you can use the scanf() function for this purpose. The scanf() function is used to read formatted input from the standard input stream (keyboard). It is a function of the C standard input/output library and defined in the stdio.h library or header file.

In this article, I shall show you how to use scanf() function in the C programming language. Here, I also show you the syntax and usage of scanf() function.

What is scanf() function :

The scanf() function is used for reading formatted input from the keyboard (standard input) in C programming language. It is part of the C standard input/output library (stdio.h). You have to carefully use this function in your C program. Using the standard input (keyboard), it reads data from the user during runtime and store it in the given variables. You have to give appropriate format specifiers according to the type and number of variables.

The scanf() function continues reading input from the keyboard (standard input) until it encounters whitespace such as space, tab or newline. It also reads input until the input by the user matches the format specifier in the formatted string.

If you want to ignore any input, you can use asterisk (*) character like below code. Here, the first input will be read but not stored in the variable.

In this code, first variable is Ignored using the asterisk (*) and then scanf() function reads the second integer variable. This is useful when you want to skip any values in the program.

When you use scanf() function, you have to handle errors carefully, especially when reading multiple values or string input. You can use the return value of scanf() for this purpose. This function returns the number of successfully matched and assigned input items.

Syntax of the scanf() function :

The following code is the syntax of the scanf() function in C programming language.

The scanf() function returns an integer value which represents the number of items successfully read and assigned to the variables.

The first parameter of scanf() function is a formatted string that specifies the format of the input. It contains format specifiers such as %d, %f, %c, %s etc. The format specifier determines the type of data to be read from the input stream (keyboard).

After that, you can pass more than one argument (pointer to the variable) where the input data will be stored. Here, you have to pass the memory address (reference) of variables. Using the address-of operator (&), you can get the memory address (reference) of variables. The number and types of pointers must match the format specifiers in the formatted string.

Use of scanf() function :

You can use scanf() function to read and store various data types such as integers, floats, characters, strings etc. You can take single or multiple inputs using scanf() function with appropriate format specifiers. In the scanf() function, you have to pass the memory address of variables where the input will be stored. Using the & operator, you can do it. In case of string input, you have to pass the array name itself because is a pointer.

The following format specifiers are used to read different types of data using scanf() function.

  • “%d” — format specifier for integer — scanf(“%d”, &int_number);
  • “%x” — format specifier for hexadecimal — scanf(“%x”, hexa_number);
  • “%o” — format specifier for octal — scanf(“%o”, octal_number);
  • “%f” — format specifier for floating-point number — scanf(“%f”, &float_number);
  • “%lf” — format specifier for double floating number — scanf(“%lf”, &double_number);
  • “%c” — format specifier for single character — scanf(” %c”, &char_input);
  • “%s” — format specifier for string — scanf(“%s”, string_input);

Using scanf() funxtion, you can read multiple values with multiple format specifiers like below code.

Here, scanf() function reads an integer, a float, and a character from the user and stores them in “int_decimal”, “float_number”, and “char_input” variables respectively.

The scanf() function automatically skips leading whitespace for most format specifiers except “%c”. When reading characters, you have to use a space before %c to ignore any leading whitespace characters.

When you read a string using “%s” specifier, you have to avoid buffer overflow by specifying a maximum width specifier like below code.

Here, the limit of input will be 9 characters plus the null terminator. So, the above code is for 10 character buffer. The scanf() function do not reads a full line. It only reads a word because it stops at whitespace.

About the program :

This is a simple C program where you can see how to use scanf() function with different data types. When you run the program on your PC, you have to enter integers, floating point numbers, single characters and string one by one. You can also see your input on the console screen after entering your input.

Explanation of the program :

At first, include stdio.h library in your program which is the standard input-output header file for printf() and scanf() functions. In the main() function, you have to declare an integer variable like “int_number”, a float variable like “float_number”, a char variable like “char_input” and a char array “string_input” to store the user inputs.

After that, ask the user for entering integer number, floating point number, single character and string using printf() function. At the same time scanf() function takes the input accordingly. In scanf() function, different types of format specifiers are used for different types of data types such as “%d” for integer, “%f” for floating-point number, “%c” for single character and “%s” for string.

In case of char input, leave a space before %c is important to handle whitespace properly like ” %c”. You have to specify width specifier (“%9s”) to read string input to prevent buffer overflow. The all different types of data will be stored in “int_number”, “float_number”, “char_input” and “string_input” variables respectively. You can display the input result on the console screen using printf() function.

How run the program :

To run the program, you have to install VS Code on your PC. You can use any other C complier (Turbo C++ IDE) for running this program. After install VS Code, open it and create new file give name as “pac.c”. Then, copy the below code and paste in the “pac.c” file of VS Code. After saving the file, run the program on your PC.

Source code of the program :

The following code is used for scanf() function in C programming language.

Output of the program :

If you run the program on your PC, you can see the below image as output.

Output of scanf() function in the C programming language

Conclusion :

Finally, you have learned how to use scanf() function in the C programming language. Now you can use scanf() function to create dynamic application where user can input during runtime. Thank you for visiting my site.

Scroll to Top