首页 > 后端开发 > C++ > 正文

在C语言中的格式说明符

PHPz
发布: 2023-09-03 14:17:05
转载
1200人浏览过

在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
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i Unsigned integer
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints 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语言免费学习笔记(深入)”;

    云雀语言模型
    云雀语言模型

    云雀是一款由字节跳动研发的语言模型,通过便捷的自然语言交互,能够高效的完成互动对话

    云雀语言模型 54
    查看详情 云雀语言模型
  • A period (.) is used to separate field width and precision

Example

 Live Demo

#include <stdio.h>
main() {
   char ch = 'B';
   printf("%c</p><p>", ch); //printing character data
   //print decimal or integer data with d and i
   int x = 45, y = 90;
   printf("%d</p><p>", x);
   printf("%i</p><p>", y);
   float f = 12.67;
   printf("%f</p><p>", f); //print float value
   printf("%e</p><p>", f); //print in scientific notation
   int a = 67;
   printf("%o</p><p>", a); //print in octal format
   printf("%x</p><p>", a); //print in hex format
   char str[] = "Hello World";
   printf("%s</p><p>", str);
   printf("%20s</p><p>", str); //shift to the right 20 characters including the string
   printf("%-20s</p><p>", str); //left align
   printf("%20.5s</p><p>", str); //shift to the right 20 characters including the string, and print string up to 5 character
   printf("%-20.5s</p><p>", str); //left align and print string up to 5 character
}
登录后复制

输出

B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
登录后复制

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

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

C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号