C conversion specifiers and modifiers

Convenient C conversion specifier and modifier reference

In this article, I want to provide allCConversionSpecifierYou can use, usually withprintf(),scanf()And similar I/O functions.

Specifier significance
%d/%i Signed decimal integer
%u Unsigned decimal integer
%c Unsignedchar
%s String
%p Pointer in hexadecimal form
%o Unsigned octal integer
%x/%X Unsigned hexadecimal number
%e Floating point number in exponential formatesymbol
%E Floating point number in exponential formatEsymbol
%f doubleNumber in decimal format
%g/%G doubleNumber in decimal format or exponential format (depending on the value)

In addition to these specifiers, we also have a set ofModifier.

let us begindigital. Use numbers in between%And format specifier, you can know the minimum field width. example:%3dNo matter how much you print, it will take up 3 spaces.

This:

printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);

Should print

   1
  12
 123
1234

If you put a dot before the digit, you are not telling the precision: the number of decimal digits. This of course applies to decimal numbers. Example:

printf("%4.2f\n", 1.0);
printf("%4.3e\n", 12.232432442);
printf("%4.1e\n", 12.232432442);
printf("%4.1f\n", 123.22);

will print:

1.00
1.223e+01
1.2e+01
123.2

In addition to digits, we have 3 special letters: h, l and L.

  • h, used with integer numbers, indicates a short int (for example %hd) or a short unsigned int (for example %hu)
  • l, used with integer numbers, indicates a long int (for example %ld) or a long unsigned int (for example %lu).
  • L, used with floating point numbers, indicates a long double, for example %Lf

Download my free C Handbook


More clang tutorials: