C printf Usage Guide
作者:XD / 发表: 2025年2月27日 05:35 / 更新: 2025年2月27日 05:38 / 编程笔记 / 阅读量:14
C printf Usage Guide
Code
# include < stdio.h>
int main() {
// Integer
int intValue = 42;
printf("Integer (%%d): %d\n", intValue);
// Float/Double
double doubleValue = 3.14159;
printf("Float/Double (%%f): %f\n", doubleValue);
// Character
char charValue = 'A';
printf("Character (%%c): %c\n", charValue);
// String
const char* stringValue = "Hello";
printf("String (%%s): %s\n", stringValue);
// Hexadecimal
int hexValue = 255;
printf("Hexadecimal (%%x): %x\n", hexValue);
// Pointer
int variable = 100;
printf("Pointer (%%p): %p\n", (void*)&variable);
// Additional examples showing formatting options
printf("\n--- Formatting Examples ---\n");
printf("Right-aligned (%%10d): '%10d'\n", intValue);
printf("Left-aligned (%%-10d): '%-10d'\n", intValue);
printf("Precision (%%0.2f): %0.2f\n", doubleValue);
printf("Width and precision (%%8.2f): '%8.2f'\n", doubleValue);
printf("Zero-padded (%%05d): %05d\n", intValue);
return 0;
}
Output
Integer (%d): 42
Float/Double (%f): 3.141590
Character (%c): A
String (%s): Hello
Hexadecimal (%x): ff
Pointer (%p): 0x7ffeeb3a1b0c (this address will vary)
--- Formatting Examples ---
Right-aligned (%10d): ' 42'
Left-aligned (%-10d): '42 '
Precision (%0.2f): 3.14
Width and precision (%8.2f): ' 3.14'
Zero-padded (%05d): 00042
相关标签