在C语言中的格式说明符

在c语言中的格式说明符

The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. Here is a list of format specifiers.

Format Specifier Type

%cCharacter%dSigned integer%e or %EScientific notation of floats%fFloat values%g or %GSimilar as %e or %E%hiSigned integer (short)%huUnsigned Integer (short)%iUnsigned integer%l or %ld or %liLong%lfDouble%LfLong double%luUnsigned int or unsigned long%lli or %lldLong long%lluUnsigned long long%oOctal representation%pPointer%sString%uUnsigned int%x or %XHexadecimal representation%nPrints nothing%%Prints % character

These are the basic format specifiers. We can add some other parts with the format specifiers. These are like below −

A minus symbol (-) sign tells left alignment

A number after % specifies the minimum field width. If string is less than the width, it will be filled with spaces

立即学习“C语言免费学习笔记(深入)”;

A period (.) is used to separate field width and precision

Example

 Live Demo

#include main() {   char ch = 'B';   printf("%c

", ch); //printing character data   //print decimal or integer data with d and i   int x = 45, y = 90;   printf("%d

", x);   printf("%i

", y);   float f = 12.67;   printf("%f

", f); //print float value   printf("%e

", f); //print in scientific notation   int a = 67;   printf("%o

", a); //print in octal format   printf("%x

", a); //print in hex format   char str[] = "Hello World";   printf("%s

", str);   printf("%20s

", str); //shift to the right 20 characters including the string   printf("%-20s

", str); //left align   printf("%20.5s

", str); //shift to the right 20 characters including the string, and print string up to 5 character   printf("%-20.5s

", str); //left align and print string up to 5 character}

登录后复制

输出

B459012.6700001.267000e+00110343Hello WorldHello WorldHello WorldHelloHello

登录后复制

我们可以以相同的方式使用这些格式说明符来使用scanf()函数。因此,我们可以像上面打印的那样从scanf()中获取输入。

以上就是在C语言中的格式说明符的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584031.html

(0)
上一篇 2025年3月6日 14:42:59
下一篇 2025年3月6日 14:43:06

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论