#include <stdio.h>
int main()
{
int a = 0;
scanf("%d", &a); // input is 45
printf("%d\n", a);
return 0;
}
45
45
Press any key to continue . . .
This function take input using standard input (keyboard) and store it in variable accordingly. It returns the number of items successfully read. Formal parameter arg1, agr2, .. must be a pointer
The given code is a C program that reads an integer from the user using the scanf() function and prints it to the console using the printf() function.
Here's how it works:
#include line includes the standard input/output library in the program.main() function is the entry point of the program.a is declared and initialized with the value 0.scanf() function is used to read an integer input from the user and store it in the variable a. The %d format specifier is used to indicate that an integer input is expected.& symbol before a in the scanf() function is used to pass the address of a as an argument. This is necessary because scanf() needs to modify the value of a.printf() function is used to print the value of a to the console using the %d format specifier.\n character is used to insert a newline after the output is printed.return 0; statement terminates the program and returns 0 to the operating system indicating that the program executed successfully.First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.