Input and Output Functions in C: printf and scanf
In C programming, input and output are fundamental and important operations. The two functions we commonly use are <span>printf</span>
and <span>scanf</span>
. This article will provide a detailed introduction to these two functions and their usage, helping beginners understand how to perform data output and input.
1. printf Function
1.1 Function Prototype
int printf(const char *format, ...);
1.2 Overview of Functionality
<span>printf</span>
is used to print formatted text to standard output (usually the screen). It can accept multiple parameters, with the first parameter being the format string, and the subsequent parameters used to replace the placeholders in the format string.
1.3 Common Placeholders
Here are some common placeholders:
<span>%d</span>
: Print integer (decimal)<span>%f</span>
: Print floating point number<span>%c</span>
: Print a single character<span>%s</span>
: Print a string<span>%%</span>
: Output the percent sign itself
1.4 Example Code
Below is a simple example program that demonstrates how to use the <span>printf</span>
function:
#include <stdio.h>
int main() {
int age = 25;
float height = 175.5;
char initial = 'A';
char name[] = "Alice";
printf("Name: %s\n", name);
printf("Initial: %c\n", initial);
printf("Age: %d years\n", age);
printf("Height: %.2f cm\n", height);
return 0;
}
Explanation of Example:
In this example, we define some variables, assign values to them, and print these values to the console by calling <span>printf</span>
.
<span>"%s"</span>
is used to display string variables.<span>"%c"</span>
is used to display a single character.<span>"%d"</span>
is used to display integers, and we also use the<span>.2f</span>
format to limit the decimal places of the floating point number.
2. scanf Function
2.1 Function Prototype
int scanf(const char *format, ...);
2.2 Overview of Functionality
In contrast to printing, the <span>scanf</span>
function is used to read data from standard input, such as keyboard input. It also uses a format string and the corresponding number of parameters to store the read data.
2.3 Common Placeholders and Address Operator
It has the following common placeholders:
<span>%d</span>
: Accept an integer and store it in a variable<span>%f</span>
: Accept a floating point number<span>%c</span>
: Accept a character<span>%s</span>
: Accept a string of characters
Note: When using <span>scanf()</span>
, you need to pass the address of the variable to store the data. This can be done using the address operator (<span>&</span>
), but for array types (such as character arrays), there is no need to use the address operator because the array name itself is a pointer to the address of the first element.
2.4 Example Code
Below is another example program that demonstrates how to use <span>scanf</span>
to get user input:
#include <stdio.h>
int main() {
int age;
float height;
printf("Please enter your age: ");
scanf("%d", &age);
printf("Please enter your height in cm: ");
scanf("%f", &height);
printf("\nYou are %d years old and %.2f cm tall.\n", age, height);
return 0;
}
Explanation of Example:
In this example, we first prompt the user to enter their age, and then use <span>scanf("%d", &age)</span>
to get and store that age value. When getting height, we similarly use <span>scanf("%f", &height)</span>
to read the floating point data, and finally print the result by calling <span>printf()</span>
.
Conclusion
This article provides a detailed introduction to the two core input and output functions in C, demonstrating their functionality and application through examples. Whether you want to display information to users or receive user input, these two functions are essential tools. Mastering these functions in actual development will lay a solid foundation for your programming. From simple to complex, you can rely on these basic tools to build rich software applications!
I hope this tutorial helps you better understand input and output operations in C. If you have any questions, feel free to try different methods or consult relevant materials to deepen your understanding and memory.