
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 printf
function 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()函数的末尾。 在此示例中,我们将打印名为name
, age
和city
变量。
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支持的格式说明符。
%c | character |
%d | decimal (integer) number (base 10) |
%e | exponential floating-point number |
%f | floating-point number |
%i | integer (base 10) |
%o | octal number (base 8) |
%s | a string of characters |
%u | unsigned decimal (integer) number |
%x | number 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具有一些特殊的字符来做出特殊的行为。 我们可以使用这些特殊字符来退格,换行,制表符,垂直制表符等。
\a | audible alert |
\b | backspace |
\f | form feed |
\n | newline, or linefeed |
\r | carriage return |
\t | tab |
\v | vertical tab |
\\ | backslash |
\一种 | 声音警报 |
\ b | 退格键 |
\F | 换页 |
\ n | 换行符或换行符 |
\ r | 回车 |
\ t | 标签 |
\ v | 垂直标签 |
\\ | 反斜杠 |
c语言printf函数原型