c语言printf函数原型_C Printf()函数示例教程

c语言printf函数原型

c语言printf函数原型

C and C++ programming language a printf function that is used to print given values or data to the standard output or current terminal. printf() function supports different formatting types. In this tutorial, we will look at them in detail.

C和C ++编程语言是printf函数,用于将给定值或数据打印到标准输出或当前端子。 printf()函数支持不同的格式类型。 在本教程中,我们将详细介绍它们。

printf()函数语法 (printf() Function Syntax)

Syntax of printf is like below.

printf语法如下。

printf(OUTPUT,DATA);

打印给定的文本和变量 (Print Given Text and Variables)

printf function can be used simply just providing a single variable. In this example, we have an integer variable named age . We print this variable with %d which is type specifier of the given variable.

仅提供一个变量就可以使用printf函数。 在此示例中,我们有一个名为age的整数变量。 我们用%d打印此变量,它是给定变量的类型说明符。

int age=20;

printf("My age is %d",age);

打印多个值 (Print Multiple Values)

We can use printffunction in order to print multiple values. We will add the variables to the end of the print() function. In this example, we will print variables named name , age and city.

我们可以使用printf函数来打印多个值。 我们将变量添加到print()函数的末尾。 在此示例中,我们将打印名为nameagecity变量。

char[] name="poftut";

int age=2;

char[] city = "ankara";

printf("Name:%s , Age:%d , City:%s",name, age, city);

打印字符串或字符数组变量 (Print String or Char Array Variable)

We have already used string or character array types to print with printf . We will use %s in order to specify string or character array type variables in an output string.

我们已经使用字符串或字符数组类型通过printf进行打印。 我们将使用%s来指定输出字符串中的字符串或字符数组类型变量。

char[] name="poftut";

printf("Name: %s",name);

打印数字或整数 (Print Numbers or Integers)

Integer variables can be printed with %d in print() function. In this example, we will print age integer variable.

可以在print()函数中使用%d打印整数变量。 在这个例子中,我们将打印age整数变量。

int age=12;

printf("Age:%d",age);

打印浮动变量和值 (Print Float Variable and Values)

Float variables type generally holds floating values. These values can be print with %f in a print() function. We will print the price floating point type variable value in this example.

浮点变量类型通常包含浮点值。 这些值可以在print()函数中用%f打印。 在此示例中,我们将打印price浮点型变量值。

int price=1.99;

printf("Age:%f",price);

打印格式说明符列表 (List Of Print Format Specifiers)

Here we can find all printf supported format specifiers.

在这里,我们可以找到所有printf支持的格式说明符。

%ccharacter
%ddecimal (integer) number (base 10)
%eexponential floating-point number
%ffloating-point number
%iinteger (base 10)
%ooctal number (base 8)
%sa string of characters
%uunsigned decimal (integer) number
%xnumber in hexadecimal (base 16)
%%print a percent sign
\%print a percent sign
%C 字符
%d 十进制(整数)数字(以10为底)
%e 指数浮点数
%F 浮点数
%一世整数(以10为底)
%o 八进制数(以8为底)
%s 一串字符
%u 无符号十进制(整数)数字
%X 十六进制数(以16为底)
%% 打印百分号
\% 打印百分号

如何打印百分号%(How To Print Percent Sign %)

As we have seen previous examples printf() function uses % as a format specifier. So there is a problem how can we print percent sign without breaking code? We can use \ to specify the percent sign is just a character in the print().

如我们所见,在前面的示例中,printf()函数使用%作为格式说明符。 因此存在一个问题,我们如何在不破坏代码的情况下打印百分号? 我们可以使用\来指定百分号只是print()中的一个字符。

printf("\% is percent sign.");

Or

要么

printf("%% is percent sign.");

左对齐打印 (Print As Left Justified)

We may need to beautify the printf() function output. The most basic beautification is aligning output. We can print given values left-justified with - and adding the space count.

我们可能需要美化printf()函数的输出。 最基本的美化是对齐输出。 我们可以用-左对齐给定的值并添加空格数。

printf("%-d",45);

填零 (Fill Zero)

We can fill integer output before given integer value. We will put 0 between % and d. In this example, we will set 3 total numbers and provide 1 .

我们可以在给定整数值之前填充整数输出。 我们将0放在%和d之间。 在此示例中,我们将设置3总数并提供1

printf("%03d", 1);

This will output following.

这将输出以下内容。

001

格式浮点 (Format Floating Point)

Floating points have two part which is decimal part and other is floating part. We can format these two-part too. We will use . and numbers to specify numbers counts. In this example, we want 4 as decimal part but 3 for the floating-point part.

浮点数有两个部分,即小数部分和另一个是浮点部分。 我们也可以将这两部分格式化。 我们将使用. 和数字以指定数字计数。 在此示例中,我们希望将4作为小数部分,将3作为浮点部分。

printf("'%4.3f'", 10.345642);

This will print only 3 number after the point.

该点之后仅打印3数字。

10.345

Printf特殊字符 (Printf Special Characters)

Printf has some special characters to make special behaviors. We can use these special characters to backspace, newline, tab, vertical tab, etc.

Printf具有一些特殊的字符来做出特殊的行为。 我们可以使用这些特殊字符来退格,换行,制表符,垂直制表符等。

\aaudible alert
\bbackspace
\fform feed
\nnewline, or linefeed
\rcarriage return
\ttab
\vvertical tab
\\backslash
\一种 声音警报
\ b 退格键
\F 换页
\ n 换行符或换行符
\ r 回车
\ t 标签
\ v 垂直标签
\\ 反斜杠
LEARN MORE  How To Setup C Development Environment
了解更多如何设置C开发环境

翻译自: https://www.poftut.com/c-printf-tutorial-examples/

c语言printf函数原型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值