Purpose of scanf in C Language
Scan Formatted String is a function in the C computer language called scanf. It reads data from the standard input stream (stdin), which is often the keyboard, and then writes the outcome into the specified parameters.
One often used input function in the C programming language is scanf(). It enables you to read data from a file or the user and store it in variables with various data kinds.
Most programs require input, and the scanf() method makes it simple to read input in several different forms. However, to guard against security flaws and unexpected program behavior, it's crucial to use scanf() with caution and to validate user input constantly.
- It uses standard input to accept character, string, and numeric data from the user.
- Like printf, scanf also makes use of format specifiers.
Syntax:
int scanf(const char *format, ...);
When an error occurs, or the input stream reaches its end, the scanf() function returns EOF, which is the number of items that were successfully read.
Parameters
The function requires two arguments:
- int: It is the return type.
- format: A string indicating the input to be read's format. Conversion specifiers, which instruct scanf() on what kind of input to expect and how to read it, may be present in this string. For further information on conversion specifiers, see the next section.
"...": An argument list with a variable length that holds the memory locations of the variables that will hold the input values. It is necessary to pass these memory addresses as pointers.
How to Read Input Using Conversion Specifiers?
The format string, which indicates the format and data types of the input that will be read, is the first argument given to the scanf() method.
Conversion specifiers, which start with the percent sign (%) and are followed by one or more characters that indicate the kind of data to be read, can be included in the format string.
The most common conversion specifiers are:
- %d: reads an integer value
- %f: reads a floating-point value
- %c: reads a single character
- %s: reads a string of characters
- %lf: reads a double-precision floating-point value
The scanf() function accepts a variable number of arguments, each of which is a pointer to the variable that will hold the input value after the format string. The format string's conversion specifiers must match the quantity and kind of arguments.
Example:
int var;
scanf(“%d”, &var);
The scanf will write the user-input value into the integer variable var.
Return Value of scanf
The scanf in C returns three types of values:
- >0: The number of values converted and assigned successfully.
- 0: No value was assigned.
- <0: Read error encountered or end-of-file(EOF) reached before any assignment was made.
Example:
#include <stdio.h>
int main()
{
int num;
float fnum;
printf("Enter an integer and a floating-point: ");
scanf("%d %f", &num, &fnum);
printf("You entered %d and %f\n", num, fnum);
return 0;
}
Output:
![Purpose of scanf in C Language/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>Here, the format string](https://static.tutorialandexample.com/c/purpose-of-scanf-in-c-language1.png)
Input Validation and Error Checking:
Programming requires an understanding of input validation and error checking, particularly when working with user or outside data. You can manage input errors and validate input in C using various approaches.
Utilizing scanf()'s return result is a popular method for determining whether an error occurred or whether the input process was successful. When an error occurs, or the input stream's end is reached, the scanf() function returns EOF, which is the number of values that were successfully read and stored.
You can ascertain whether an error occurred or whether the input procedure was successful by examining the return value.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
printf("Enter an integer: ");
if (scanf("%d", &num) != 1) {
printf("Error: Invalid input\n");
exit(1);
}
return 0;
}
Output:
![Purpose of scanf in C Language/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>In this example, an integer number is read and stored in the num variable using the scanf() method. To see if one input value was correctly read and stored, the scanf() return value is compared to 1. The program ends with an error code and prints an error message to the console if the return value is not 1.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Similar methods can be applied to validate various forms of input, including strings or floating-point values. For instance, you could use the %f conversion specifier to examine the input of a floating-point value and see if scanf()'s return value equals 1.</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Apart from verifying the return value of scanf(), there exist alternative methods for validating input and managing errors. These methods include reading input as a string using fgets(), parsing the string to extract the required data, and validating input patterns using regular expressions.</p>
<!-- /wp:paragraph --></p>
</div>
<div class=](https://static.tutorialandexample.com/c/purpose-of-scanf-in-c-language2.png)