Thursday, January 16, 2025
HomeProgrammingWhat is scanf in C

What is scanf in C

In C, the scanf function is used to read input from the user through the standard input (keyboard). It allows the program to capture various data types like integers, floating-point numbers, characters, and strings.

Syntax:

scanf(“format_specifier”, &variable1, &variable2, …);

See also  What is the difference between declarative and imperative programming?

The format specifier indicates the type of data expected (e.g., %d for integers, %f for floats, %s for strings).

The & operator is used to pass the address of the variable to scanf.

Example:

#include <stdio.h>

int main() {
int age;
float height;

See also  Spiral Model (Software Engineering)

printf(“Enter age and height: “);
scanf(“%d %f”, &age, &height);

printf(“Age: %d, Height: %.2f\n”, age, height);
return 0;
}

In this example, scanf reads an integer and a float from the user. It’s important to ensure that the correct format specifiers are used to avoid input errors.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x