博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
printf中的指针变量_C语言中的printf()示例/变量
阅读量:2529 次
发布时间:2019-05-11

本文共 6405 字,大约阅读时间需要 21 分钟。

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 worldHow 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

输出量

HelloLength: 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 
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 worldHow are you?Hello "World", How are you?Hey I got 84.20% in my final examsnum in octal format: 377num in hexadecimal format(lowercase) : ffnum in hexadecimal format(uppercase) : FFHello world, how are you?    I love C programing language.The file is store at c:\files\word_filesHelloLength: 6num (padded): 00255str1="               Hello", str2="World               "val = 1.23num = 255Address of num is: 0x7ffca0b5855c

翻译自:

printf中的指针变量

转载地址:http://gkxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>