当前位置:网站首页>C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
2022-06-23 03:58:00 【Charles Ren】
常量和变量
明示常量
明示常量又叫符号常量,define修饰的字符
#define TAX 0.15
在编译程序时就会将TAX都替换成0.15。在运行程序前所有的替换已经完成。
作用:提高程序的可读性和可维护性。
const修饰的变量
const修饰的是变量,但是是只读的。
他是运行期才分配内存。存储位置和常量也不同。
常用常量


转换的意义
转换说明把以二进制格式存储在计算机中的值,转换成字符串便于显示。
比如76这个值,他在计算机中就是01001100。
使用%d的意识是“把给定的值翻译成十进制文本并打印出来”。所以%d将其转换成字符7和6并显示为76。
printf 打印类型
printf打印不同类型值所使用的符号,使用错误的说明符会打印意想不到的结果。printf("%c %d", '$', 2*a) printf打印的是值,无论变量,常量,还是表达式。
整型转换
| 转换说明 | 输出 |
|---|---|
| %d | 十进制显示,可打印int 和short,也可以写做%i |
| %ld | long |
| %lld | long long int |
| %o | 八进制int |
| %lo | 八进制long |
| %x | 十六进制int |
| %lx | 十六进制long |
| %#o | 八进制加前缀0显示 |
| %#x | 十六进制加前缀0x显示 |
| %#X | 十六进制加前缀0X显示 |
| %u | unsigned int |
| %lu | unsigned long |
| %4d | 最小的字段宽度,如果不够容纳待打印的数,则自动扩充 |
| %zd | 或者%zu,表示size_t类型的值,sizeof()或者strlen()运算返回的结果类型。在早期C用%ul来接收 |
浮点数转换
| 转换说明 | 输出 |
|---|---|
| %f | 浮点数十进制记数法表示float或double,float会被自动转为double输出 |
| %6.2f | 表明待打印的数⾄少占6个字符宽度,且⼩数点后⾯有2位⼩数,⼩数点占⼀位,所以整数部分⾄少占3位 |
| %.2f | 表示整数部分正常,小数部分保留2位他和%0.2f含义相同 |
| %Lf | long double 这个要大写 |
| %e | 使用e计数法来表示浮点数 |
| %g | 根据值自动选择%f或者%e |
打印不同精度的浮点数
https://blog.csdn.net/dodamce/article/details/115297198
其他转换
| 转换说明 | 输出 |
|---|---|
| %c | 单个字符 |
| %s | 字符字符串 |
| %p | 指针的值 |
| %% | 打印一个百分号 |
| \n | 换行 |
| \\ | 斜杠 |
| \’ | 单引号 |
| \" | 双引号 |
标记

#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);//打印10个字符宽度
printf("*%-10d*\n", PAGES);//左对齐
return 0;
}
//*959*
//*959*
//* 959*
//*959 *
// floats.c -- some floating-point combinations
#include <stdio.h>
int main(void)
{
const double RENT = 3852.99; // const-style constant
printf("*%f*\n", RENT); // 保留6位小数
printf("*%e*\n", RENT); //e表示法
printf("*%4.2f*\n", RENT); //4宽度,2位小数,宽度不够则显示原数
printf("*%3.1f*\n", RENT);
printf("*%10.3f*\n", RENT); //10宽度,3位小数
printf("*%-10.3f*\n", RENT); //左对齐
printf("*%10.3E*\n", RENT);
printf("*%+4.2f*\n", RENT); //显示符号
printf("*%010.2f*\n", RENT); //用0填充不够的位
return 0;
}
// *3852.990000*
// *3.852990e+03*
// *3852.99*
// *3853.0*
// * 3852.990*
// *3852.990 *
// * 3.853E+03*
// *+3852.99*
// *0003852.99*
打印字符串
打印较长字符串换行有三种方法,
- 使用多个printf语句。
- 使用反斜杠,但是第二行必须左对其,不然就会有空格
- (推荐)使用双引号链接符
int main(void)
{
printf("Here's one way to print a ");
printf("long string.\n");
printf("Here's another way to print a \ long string.\n");
printf("Here's the newest way to print a "
"long string.\n"); /* ANSI C */
return 0;
}
scanf函数
C语言库有多个输入函数,但是scanf是最通用的一个,因为他可以读取不同格式的数据。
scanf和printf使用方式基本一样,但是注意一点。scanf函数使用的是指向变量的指针。
也就是使用的时候注意两个技巧:
- scanf读取基本变量类型的值在变量名前面一个&
- 把字符串读入字符数组,变量名前不用&
scanf函数使用空白(换行符,制表符和空格)把输入分成多个字段。也就是用空白作为分隔符。
scanf所使用的转换说明与printf相同,见上面表格。唯一一点不同,scanf会将所有输入的浮点数类型都应用到float类型上。
举例
scanf读取一个%d,也就是读取一个整数:
- 他会跳过输入的空白字符,从第一个非空白字符开始读取
- 然后读取数字字符,直到遇到非数字字符,他便认为读到了整数的末尾。
- 然后将该数字放入到变量。
如果使用%s转换说明,scanf会读取空白以外的所有字符。
- 他会跳过输入的空白字符,从第一个非空白字符开始读取
- 依次读取字符直到再遇到空白,停止。
其他输入函数
我们看到如果我要读取带空格的字符串使用scanf就无法读取了。
使用getchar()和fgets()更适合处理一些字符串的特殊情况。后面会再讲。
只是scanf是一种更通用的输入方式。
格式输入
scanf("%d,%d:, &n, &m);
则输入必须以下形式
55,45
scanf("%d %d:, &n, &m);
则输入必须是一下形式
55 45
边栏推荐
- 高等数学(第七版)同济大学 习题1-8 个人解答
- Win11 app store keeps turning around solution
- hash---------history
- Jetpack compose menubar Desktop Menu from door opening to entry
- Is there a real part-time job online? How do college students find part-time jobs in summer?
- Go grouping & sorting
- Heimdall Database Proxy横向扩展提高20倍
- sprintf 格式代码使用不规范在不同平台下的表现
- 云原生数据库是未来数据库的天下
- MCS: discrete random variable Bernoulli distribution
猜你喜欢

konva 系列教程 1:konva 是什么?

Konva series tutorial 1:what is konva?

Markdown add background color to the picture

GDB data reading in GDAL (III) of GIS
![[opencv450] inter frame difference method](/img/ad/c8a56e27d78cea581deb1874620613.png)
[opencv450] inter frame difference method

Qimen dunjia assistant decision software

Cloud native database is the world of future databases

Redis缓存穿透解决方案-布隆过滤器

Jenkins installs and deploys and automatically builds and publishes jar applications

MCS: continuous random variable - student's t distribution
随机推荐
CF [1700d] D. River locks (DP, bisection, Mathematics)
英集芯推出4串锂电池100W移动电源升降压方案SoC芯片IP5389
Facing new challenges and becoming a better self -- an advanced technology er
电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
fastjson中的@JSONField注解
高等数学(第七版)同济大学 习题1-8 个人解答
IP6809三线圈15W无线充电发射端方案ic英集芯
Face recognition determination threshold
Xa mode explanation and code implementation of four Seata modes
H5 adaptive full screen
What is the reason for the black screen of the computer monitor when the computer is turned on? What should I do about the black screen of the computer monitor
ES6的Array.from方法创建长度为N的undefined数组
低成本5W无线充电器方案FS68001B简便充电芯片
What if win11 cannot record audio? Solution of win11 unable to input sound
JDBC introductory learning (II) encapsulation tool class
奇门遁甲辅助决策软件
LeetCode-1757. Recyclable and low-fat products_ SQL
STC 32 Bit 8051 Single Chip Computer Development Example Tutorial one development environment
ArcTime 制作中英文字幕视频
Markdown add background color to the picture