printf中的指针变量_C语言中的printf()示例/变量

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中的指针变量

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值