Home >Common Problem >What is the usage of printf function in c program

What is the usage of printf function in c program

zbt
zbtOriginal
2023-09-28 09:45:591315browse

C language is a widely used programming language, and the printf function is one of the most commonly used functions in C language. It is used to output text and variable values ​​to the screen. In this article, we will detail the usage of the printf function and some common formatting options.

First, let's take a look at the basic syntax of the printf function:

int printf(const char *format, ...);

The printf function accepts a format string as the first parameter, which specifies the format of the output. In the format string, you can insert placeholders that specify the type and format of the variable to be output. Placeholders begin with a percent sign (%) and are followed by one or more characters that specify the type and format of the variable.

The following are some common placeholders and their usage:

- %d: used to output integers.

- %f: used to output floating point numbers.

- %c: used to output characters.

- %s: used to output string.

In addition to these basic placeholders, the printf function also supports some formatting options for specifying the format of the output. Here are some common formatting options:

- d: Specifies the output field width to be 10 characters. If the output integer is not wide enough, spaces will be padded on the left.

- %-10d: Specifies that the output field width is 10 characters. If the output integer is not wide enough, spaces will be padded on the right.

- %.2f: Specify two digits after the decimal point of the output floating point number.

- .2f: Specifies that the output field width is 10 characters, and two decimal places are retained for floating point numbers.

The following are some examples demonstrating the usage of the printf function:

int main() {
int num = 10;
float pi = 3.14159;
char letter = 'A';
char name[] = "John";
printf("The number is %d\n", num);
printf("The value of pi is %.2f\n", pi);
printf("The letter is %c\n", letter);
printf("The name is %s\n", name);
return 0;
}

Running the above code will output the following results:

The number is 10
The value of pi is 3.14
The letter is A
The name is John

In the above example, we use Different placeholders and formatting options are used to output different types of variables. Note that the order of placeholders in the format string must match the order of variables.

In addition, the printf function also supports some other escape characters for outputting special characters, such as newline (\n), tab (\t), etc. These escape characters can be used directly in the format string.

To sum up, the printf function is an important function in C language for outputting text and variable values. By using different placeholders and formatting options, we can flexibly control the format of the output. I hope this article can help you better understand and use the printf function .

The above is the detailed content of What is the usage of printf function in c program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn