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!

The domestic AI dark horse DeepSeek has risen strongly, shocking the global AI industry! This Chinese artificial intelligence company, which has only been established for a year and a half, has won wide praise from global users for its free and open source mockups, DeepSeek-V3 and DeepSeek-R1. DeepSeek-R1 is now fully launched, with performance comparable to the official version of OpenAIo1! You can experience its powerful functions on the web page, APP and API interface. Download method: Supports iOS and Android systems, users can download it through the app store; the web version has also been officially opened! DeepSeek web version official entrance: ht

DeepSeek: How to deal with the popular AI that is congested with servers? As a hot AI in 2025, DeepSeek is free and open source and has a performance comparable to the official version of OpenAIo1, which shows its popularity. However, high concurrency also brings the problem of server busyness. This article will analyze the reasons and provide coping strategies. DeepSeek web version entrance: https://www.deepseek.com/DeepSeek server busy reason: High concurrent access: DeepSeek's free and powerful features attract a large number of users to use at the same time, resulting in excessive server load. Cyber Attack: It is reported that DeepSeek has an impact on the US financial industry.

At the beginning of 2025, domestic AI "deepseek" made a stunning debut! This free and open source AI model has a performance comparable to the official version of OpenAI's o1, and has been fully launched on the web side, APP and API, supporting multi-terminal use of iOS, Android and web versions. In-depth search of deepseek official website and usage guide: official website address: https://www.deepseek.com/Using steps for web version: Click the link above to enter deepseek official website. Click the "Start Conversation" button on the homepage. For the first use, you need to log in with your mobile phone verification code. After logging in, you can enter the dialogue interface. deepseek is powerful, can write code, read file, and create code

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use