printf中的指針變量
As we know that, printf() is used to print the text and value on the output device, here some of the examples that we wrote to use the printf() in a better way or for an advance programming.
眾所周知, printf()用于在輸出設備上打印文本和值,這里是我們編寫的一些示例,它們以更好的方式或為了進行高級編程而使用了printf() 。
1) Print normal text
1)打印普通文字
printf ("Hello world");
Output
輸出量
Hello world
2) Print text in new line
2)在新行中打印文本
printf ("Hello world\nHow are you?");
Output
輸出量
Hello world
How are you?
3) Print double quote
3)打印雙引號
To print double quote, we use \".
要打印雙引號,我們使用\“ 。
printf("Hello \"World\", How are you?\n");
Output
輸出量
Hello "World", How are you?
4) Print percentage sign (%)
4)列印百分比符號(%)
To print percentage sign/ character, we use %%.
要打印百分比符號/字符,我們使用%% 。
printf ("Hey I got 84.20%% in my final exams\n");
Output
輸出量
Hey I got 84.20% in my final exams
5) Print octal value
5)打印八進制值
To print octal value, we use %o (It's alphabet o in lowercase)
要打印八進制值,我們使用%o (小寫的字母o)
int num = 255;
printf("num in octal format: %o\n", num);
Output
輸出量
num in octal format: 377
6) Print hexadecimal value (with lowercase alphabets)
6)打印十六進制值(使用小寫字母)
To print hexadecimal value, we use %x (its alphabet 'x' in lowercase) - as we know that a hexadecimal value contains digits from 0 to 9 and alphabets A to F, "%x" prints the alphabets in lowercase.
要打印十六進制值,我們使用%x (小寫的字母“ x”)-因為我們知道十六進制值包含從0到9的數字和字母A到F, “%x”以小寫字母打印。
int num = 255;
printf ("num in hexadecimal format(lowercase) : %x\n", num);
Output
輸出量
num in hexadecimal format(lowercase) : ff
7) Print hexadecimal value (with uppercase alphabets)
7)打印十六進制值(使用大寫字母)
To print hexadecimal value, we use %X (it's alphabet X in uppercase) - as we know that a hexadecimal value contains digits from o to 9 and alphabets A to F, "%X" prints the alphabets in uppercase.
要打印十六進制值,我們使用%X (大寫字母X)-因為我們知道十六進制值包含從o到9的數字和字母A至F, “%X”將大寫字母。
int num = 255;
printf ("num in hexadecimal format(uppercase) : %X\n", num);
Output
輸出量
num in hexadecimal format(uppercase) : FF
8) Print long string using \ (slash)
8)使用\(斜杠)打印長字符串
If there is a long string, that you want to print with a single printf() with two or more lines, we can use slash (\).
如果有一個很長的字符串,而您想使用帶有兩行或更多行的單個printf()進行打印,則可以使用斜杠( \ )。
printf ("Hello world, how are you?\
I love C programing language.\n");
Output
輸出量
Hello world, how are you? I love C programing language.
9) Print backslash (\)
9)打印反斜杠(\)
To print backslash (\), we use double backslash (\\).
要打印反斜杠( \ ),我們使用雙反斜杠( \\ )。
printf ("The file is store at c:\\files\\word_files\n");
Output
輸出量
The file is store at c:\files\word_files
10) To get total number of printed characters
10)獲取打印字符總數
We can also get the total number of printed character using printf(), printf() returns the total number of printed character, that we can store in a variable and print.
我們還可以使用printf()獲得打印字符的總數, printf()返回打印字符的總數,我們可以將其存儲在變量中并進行打印。
int len = 0;
len = printf ("Hello\n");
printf ("Length: %d\n", len);
Output
輸出量
Hello
Length: 6
11) Print integer value 5 digit left padded with 0
11)打印整數值左5位填充0
To print value with 0 padded in 5 digits, we can use "%05d".
要打印以5位數字填充0的值,我們可以使用“%05d” 。
int num = 255;
printf ("num (padded): %05d\n", num);
Output
輸出量
num (padded): 00255
12) Print text with left and right padding
12)使用左右填充符打印文本
To print left padded text with space, we use "%20s" - Here, 20 is the number of characters, if string contains 5 characters then 15 spaces will be added at the left of the text.
要打印帶有空格的左填充文本,我們使用“%20s” -這里的字符數為20,如果字符串包含5個字符,則將在文本左側添加15個空格。
Similarly, to print right padded text with space, we use a flag "-" like "%-20s" - Here, 20 is the number of characters, if string contains 5 characters then 15 spaces will be added at the right of the text.
類似地,要打印帶有空格的右填充文本,我們使用標記“-”,例如“%-20s” -這里的20是字符數,如果字符串包含5個字符,則將在文本右邊添加15個空格。
printf ("str1=\"%20s\", str2=\"%-20s\"\n", "Hello", "World");
Output
輸出量
str1=" Hello", str2="World "
13) Print float value to specified number of digits after the decimal point
13)將浮點值打印到小數點后的指定位數
To print float value with specified number of digits after the decimal, we use "%.2f". Here, 2 is the number of digits after the decimal point.
要在小數點后打印具有指定位數的浮點值,請使用“%.2f” 。 在此,2是小數點后的位數。
float val = 1.234567;
printf("val = %.2f\n", val);
Output
輸出量
val = 1.23
14) Print integer value with "%i" format specifier
14)使用“%i”格式說明符打印整數值
While printing the value "%d" and "%i" are same, they are used to print an integer value, we can also print the integer value by using "%i".
雖然打印值“%d”和“%i”相同,但是它們用于打印整數值,我們也可以使用“%i”打印整數值。
int num = 255;
printf("num = %i \n", num);
Output
輸出量
num = 255
15) “%p” can be used to print the address of a variable
15)“%p”可用于打印變量的地址
Since, address of the variable is a large hexadecimal value - to print it we should not (or cannot) use "%d", "%i" etc. To print an address of a variable, we must use "%p".
由于變量的地址是一個大的十六進制值-要打印它,我們不應該(或不能)使用“%d” , “%i”等。要打印變量的地址,我們必須使用“%p” 。
int num = 255;
printf("Address of num is: %p\n", &num);
Output
輸出量
Address of num is: 0x7ffca0b5855c
上述所有printf()語句的示例 (Example with all above printf() statements )
#include <stdio.h>
int main()
{
int num = 255;
int len = 0;
float val = 1.234567;
printf("Hello World");
printf("Hello world\nHow are you?");
printf("Hello \"World\", How are you?\n");
printf ("Hey I got 84.20%% in my final exams\n");
printf("num in octal format: %o\n", num);
printf ("num in hexadecimal format(lowercase) : %x\n", num);
printf ("num in hexadecimal format(uppercase) : %X\n", num);
printf ("Hello world, how are you?\
I love C programing language.\n");
printf ("The file is store at c:\\files\\word_files\n");
len = printf ("Hello\n");
printf ("Length: %d\n", len);
printf ("num (padded): %05d\n", num);
printf ("str1=\"%20s\", str2=\"%-20s\"\n", "Hello", "World");
printf("val = %.2f\n", val);
printf("num = %i \n", num);
printf("Address of num is: %p\n", &num);
return 0;
}
Output
輸出量
Hello WorldHello world
How are you?Hello "World", How are you?
Hey I got 84.20% in my final exams
num in octal format: 377
num in hexadecimal format(lowercase) : ff
num in hexadecimal format(uppercase) : FF
Hello world, how are you? I love C programing language.
The file is store at c:\files\word_files
Hello
Length: 6
num (padded): 00255
str1=" Hello", str2="World "
val = 1.23
num = 255
Address of num is: 0x7ffca0b5855c
翻譯自: https://www.includehelp.com/c-programs/printf-examples-variations.aspx
printf中的指針變量