方便的C轉換說明符和修飾符參考
在這篇文章中,我想為所有C轉換說明符您可以使用,通常與printf()
,scanf()
和類似的I / O功能。
說明符 | 意義 |
---|---|
%d /%i |
有符號十進制整數 |
%u |
無符號十進制整數 |
%c |
無符號char |
%s |
細繩 |
%p |
十六進制形式的指針 |
%o |
無符號八進制整數 |
%x /%X |
無符號十六進制數 |
%e |
指數格式中的浮點數e 符號 |
%E |
指數格式中的浮點數E 符號 |
%f |
double 十進制格式的數字 |
%g /%G |
double 十進制格式或指數格式的數字(取決於值) |
除了這些說明符,我們還有一組修飾符。
讓我們開始數字。在之間使用數字%
和格式說明符,您可以知道最小字段寬度。例子:%3d
不管打印多少,都將佔用3個空格。
這:
printf("%4d\n", 1);
printf("%4d\n", 12);
printf("%4d\n", 123);
printf("%4d\n", 1234);
應該打印
1
12
123
1234If 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.2In 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:
- Introduction to the C Programming Language
- C Variables and types
- C Constants
- C Operators
- C Conditionals
- How to work with loops in C
- Introduction to C Arrays
- How to determine the length of an array in C
- Introduction to C Strings
- How to find the length of a string in C
- Introduction to C Pointers
- Looping through an array with C
- Booleans in C
- Introduction to C Functions
- How to use NULL in C
- Basic I/O concepts in C
- Double quotes vs single quotes in C
- How to return a string from a C function
- How to solve the implicitly declaring library function warning in C
- How to check a character value in C
- How to print the percentage character using `printf()` in C
- C conversion specifiers and modifiers
- How to access the command line parameters in C
- Scope of variables in C
- Can you nest functions in C?
- Static variables in C
- C Global Variables
- The typedef keyword in C
- C Enumerated Types
- C Structures
- C Header Files
- The C Preprocessor