C conversion specifiers and modifiers
A handy reference to C conversion specifiers and modifiers
In this post I want to create a helpful reference for all the C conversion specifiers you can use, commonly with printf(), scanf() and similar I/O functions.
| Specifier | Meaning |
|---|---|
%d / %i | Signed decimal integer |
%u | Unsigned decimal integer |
%c | Unsigned char |
%s | String |
%p | Pointer in hexadecimal form |
%o | Unsigned octal integer |
%x / %X | Unsigned hexadecimal number |
%e | Floating point number in exponential format in e notation |
%E | Floating point number in exponential format in E notation |
%f | double number in decimal format |
%g / %G | double number in decimal format or exponential format depending on the value |
In addition to those specifiers, we have a set of modifiers.
Let’s start with digits. Using a digit between % and the format specifier, you can tell the minimum field width. Example: %3d will take 3 spaces regardless of the number printed.
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 ashort int(for example%hd) or ashort unsigned int(for example%hu)l, used with integer numbers, indicates along int(for example%ld) or a long unsigned int (for example%lu).L, used with floating point numbers, indicates along double, for example%Lf
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.