printf() and scanf() in C
In C programming, printf() and scanf() are essential functions for handling input and output.
printf()
The printf() function is used to display formatted output to the console. Its syntax is:
c
int printf(const char *format, ...);
The format string contains format specifiers that determine how the subsequent arguments are presented. Common specifiers include ?or integers, ?or floating-point numbers, ?or characters, and %s for strings. For example:
c
printf("Age: %d, Height: %.1f\n", age, height);
This displays the values of age and height in a formatted manner.
scanf()
The scanf() function reads formatted input from the user. Its syntax is:
c
int scanf(const char *format, ...);
Similar to printf(), it uses format specifiers to identify the type of data expected. For example, %d reads an integer and %f reads a float. The variables where the input will be stored must be passed by reference using the address-of operator (&):
c
scanf("%d", &age);
Together, printf() and scanf() provide a robust way to interact with users, enabling effective input and output handling in C programs.