博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言双精度的格式说明符_C中的格式说明符
阅读量:2528 次
发布时间:2019-05-11

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

c语言双精度的格式说明符

Format specifiers in C are used to accept and display data to the user. In programming terms, format specifiers help the compiler analyze the type of data being provided to the program.

C语言中的格式说明符用于接受数据并向用户显示数据。 用编程术语来说,格式说明符可帮助编译器分析提供给程序的数据类型。

Thus, it helps to find out the associated with the corresponding variable of the program.

因此,它有助于找出与程序的相应变量关联的 。

Points to Remember:

要记住的要点:

There are basically three elements that rule the format specifiers:

基本上有三个元素统治格式说明符:

  1. A period (.) – Separates field width and provides precision.

    句点(。)–分隔字段宽度并提供精度。
  2. A minus symbol (-) – Provides left alignment.

    减号(-)–提供左对齐。
  3. A number after the ‘%’ to specify the minimum width of the string to be printed.

    '%'之后的数字,指定要打印的字符串的最小宽度。


C格式说明符列表 (List of format specifiers in C)

Format Specifier Description
%c Accepts and prints characters
%d Accepts and prints signed integer
%e or %E Scientific notation of floats
%f Accepts and prints float numbers
%hi Prints the signed integer
%hu Prints unsigned integer
%i Accepts and prints integer values
%l or %ld or %li Long integer values
%lf Double values
%Lf Long double values
%lu Unsigned int or unsigned long
%o Provides the octal form of representation
%s Accepts and prints String values
%u Accepts and prints unsigned int
%x or %X Provides the hexadecimal form of representation
%n Provides a new line
%% This prints % character
格式说明符 描述
%C 接受并打印字符
%d 接受并打印带符号的整数
%e或%E 浮点数的科学表示法
%F 接受并打印浮点数
%hi 打印有符号整数
%hu 打印无符号整数
%一世 接受并打印整数值
%l或%ld或%li 长整数值
%如果 双重值
%如果 长双精度值
%鲁 Unsigned int或unsigned long
%o 提供八进制表示形式
%s 接受并打印字符串值
%u 接受并打印unsigned int
%x或%X 提供十六进制表示形式
%n 提供新行
%% 打印%字符


整数格式说明符(%d或%i或%u) (Integer format specifier(%d or %i or %u))

Integer format specifiers accept and print integer values through the program.

整数格式说明符通过程序接受并打印整数值。

Example:

例:

#include 
int main() { int x = 0; printf("Enter the value of x:\n"); scanf("%d", &x); printf("%d\n", x); int z = 0; printf("Enter the value of z:\n"); scanf("%i", &z); printf("%i\n", z); return 0; }

Output:

输出:

Enter the value of x:1010Enter the value of z:2525

The “%d” format specifier takes the input number in a decimal format while the “%i” format specifier takes the input number into decimal or octal or hexadecimal format.

“%d”格式说明符将输入数字采用十进制格式,而“%i”格式说明符将输入数字采用十进制或八进制或十六进制格式。



字符格式说明符(%c) (Character format specifier(%c))

Character format specifiers accept and print character values through the program.

字符格式说明符接受并通过程序打印字符值。

Example:

例:

#include 
int main() { char x = ""; printf("Enter the character:\n"); scanf("%c", &x); printf("The entered character is:\n"); printf("%c\n", x); return 0; }

Output:

输出:

Enter the character:DThe entered character is:D


八进制格式说明符(%o) (Octal format specifier(%o))

Octal format specifiers help provide an octal representation of the given integer.

八进制格式说明符有助于提供给定整数的八进制表示形式。

Example:

范例

#include 
int main() { int num = 69; printf("The octal form of the input:\n"); printf("%o\n", num); return 0; }

Output:

输出:

The octal form of the input:105


浮点格式说明符(%f或%e或%E) (Float format specifier(%f or %e or %E))

Example:

例:

#include 
int main(){ float num = 1.24578; printf("%f\n", num); printf("%0.2f\n", num); printf("%e\n", num); return 0;}

In the above piece of code, %0.2f provides the precision of up to two decimal values.

在上面的代码中,%0.2f提供了最多两个十进制值的精度。

Output:

输出:

1.2457801.251.245780e+00


十六进制格式说明符(%x或%X) (Hexadecimal format specifier(%x or %X))

Hexadecimal format specifiers provide the hexadecimal representation of the given integer.

十六进制格式说明符提供给定整数的十六进制表示形式。

Example:

例:

#include 
int main(){ int num = 13; printf("%x\n", num); return 0;}

Output:

输出:

d


字符串格式说明符(%s) (String format specifier(%s))

These format specifiers accept and print character arrays or strings through the program.

这些格式说明符通过程序接受并打印字符数组或字符串。

Example:

例:

#include 
int main(){ char in_str[] = "Engineering Discipline"; printf("%s\n", in_str); return 0;}

Output:

输出:

Engineering Discipline


结论 (Conclusion)

Thus, in this article, we have understood the role of format specifiers in the C language.

因此,在本文中,我们了解了C语言中格式说明符的作用。



参考资料 (References)

翻译自:

c语言双精度的格式说明符

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

你可能感兴趣的文章
SELINUX setsebool
查看>>
C#的Socket程序(TCP/IP)总结
查看>>
java源码生成可运行jar
查看>>
用一个常见的生活小例子来解释和演示面向接口编程
查看>>
找出数组中两个只出现一次的数字
查看>>
【TYVJ水题三连发】1502 兴建高铁 1568 Rabbit Number 1673 位图
查看>>
centos 允许远程连接mysql
查看>>
C#之用XmlWriter保存XML数据
查看>>
光和颜色
查看>>
Metro UI风格配色方案
查看>>
WGAN (原理解析)
查看>>
PHP json_encode中文乱码解决方法
查看>>
解决ie6、ie7下float为right换行的情况
查看>>
Windows搭建测试RabbitMq遇到的问题
查看>>
Net分布式系统之三:Vm安装配置Nginx
查看>>
题目搜集
查看>>
[BZOJ 1055] [HAOI2008] 玩具取名 【记忆化搜索】
查看>>
travis CI
查看>>
2nd 词频统计更新
查看>>
转载------------java 进程创建
查看>>