SoFunction
Updated on 2025-05-23

C/C++ format specifier and its usage

In C/C++, Format Specifiers are mainly used forprintf()scanf()In other input and output functions, it is used to control the formatted input and output of data. The following are common format specifiers and their usage:

1. Basic format specifier

Explanation Applicable data types describe
%d int Decimal integer (signed)
%u unsigned int Decimal unsigned integers
%f float Floating point number (6 decimal places are retained by default)
%lf double Double precision floating point number (printfAvailable in%fInstead, butscanfMust use%lf
%c char Single character
%s char*(C string) String (need to\0End)
%p void* Pointer address (hexadecimal format, such as0x7ffeeb0b4d60
%x int, unsigned int Hexadecimal integers (lowercase letters, such as1a3f
%X Same as above Hexadecimal integer (capsular letters, such as1A3F
%o Same as above Octal integers
%e float, double Scientific notation method (lowercasee,like3.141593e+00
%E Same as above Scientific Counting Method (UppercaseE,like3.141593E+00
%g/%G Same as above Automatic selection%for%e(Determine the more compact format based on the size of the value)
%% - Output percent sign%

2. Modifier (used to fine control format)

1. Width and alignment

printf("%10d", 123);   // The output width is 10, right-aligned: "123"printf("%-10d", 123);  // The output width is10,Left aligned:"123       "

2. Accuracy control

printf("%.2f", 3.14159); // Keep 2 decimal places: 3.14printf("%.5s", "hello world"); // Before intercept5Characters:"hello"

3. Fill characters

printf("%05d", 42);    // use0Fill Width5:"00042"

4. Length modifier

Modifier Example describe
h %hd, %hu short int / unsigned short
l %ld, %lu long int / unsigned long
ll %lld, %llu long long int / unsigned long long
L %Lf, %Le long double

3. The format specifier of scanf()

scanf()The data type must be strictly matched:

int num;
scanf("%d", &num);  // Enter integerdouble value;
scanf("%lf", &value); // Enter a double precision floating point number(Must use%lf)

Skip input

scanf("%d %*s %f", &age, &salary); // Skip the middle string input

4. Code examples

#include <>
int main() {
    int num = 42;
    float pi = 3.14159;
    char str[] = "Hello";
    printf("Decimal: %d, Hexadecimal: 0x%x\n", num, num);
    printf("Floating point number: %.2f, scientific notation method: %e\n", pi, pi);
    printf("String: %s, address: %p\n", str, (void*)&num);
    printf("Fill example: %05d\n", num);
    return 0;
}

Output

Decimal: 42, Hexadecimal: 0x2a
Floating point number: 3.14, Scientific notation method: 3.141590e+00
String: Hello, address: 0x7ffd5a3d4a4c
Fill example: 00042

5. Things to note

  • Type Match: The format specifier must strictly match the variable type, otherwise it may result in undefined behavior.
    • Error example:printf("%d", 3.14);// mistake! application%f
  • Pointer and address%pNeed to cast the pointer tovoid*
  • String securityscanf("%s", str)It may cause buffer overflow, it is recommended to use%Ns(like%10s) Limit the input length.

This is the end of this article about C/C++ format specifiers. For more related C++ format specifier content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!