printf函數是C語言中用于輸出內容的函數,其格式化輸出的用法如下:
輸出字符串: printf(“Hello World\n”);
輸出整數: int num = 10; printf(“Number: %d\n”, num);
輸出浮點數: float floatNum = 3.14; printf(“Float Number: %.2f\n”, floatNum);
輸出字符: char ch = ‘A’; printf(“Character: %c\n”, ch);
輸出字符串變量: char str[] = “C Programming”; printf(“String: %s\n”, str);
輸出指針變量: int *ptr = # printf(“Pointer: %p\n”, ptr);
輸出十六進制數: int hexNum = 0x1F; printf(“Hexadecimal Number: %X\n”, hexNum);
輸出八進制數: int octNum = 037; printf(“Octal Number: %o\n”, octNum);
輸出逃逸字符: printf(“New Line: \n”); printf(“Tab: \t”); printf(“Backslash: \”); printf(“Double Quotes: "”); printf(“Single Quote: '”);
輸出格式化字符串: int num1 = 10, num2 = 20; printf(“Sum of %d and %d is %d\n”, num1, num2, num1 + num2);
以上是printf函數的常用用法,通過使用不同的格式控制符,可以輸出不同類型的數據。