当前位置:网站首页>C primer plus learning notes - 2. Constant and formatted IO (input / output)
C primer plus learning notes - 2. Constant and formatted IO (input / output)
2022-06-23 05:41:00 【Charles Ren】
List of articles
Constants and variables
Explicit constants
Explicit constants are also called symbolic constants ,define Decorated characters
#define TAX 0.15
When the program is compiled, it will TAX Are replaced with 0.15. All replacements have been completed before running the program .
effect : Improve the readability and maintainability of the program .
const Decorated variable
const The modifier is the variable , But it's read-only .
It allocates memory only at runtime . Storage locations and constants are also different .
Common constants


The meaning of transformation
Conversion instructions store values in binary format in a computer , Convert to string for easy display .
such as 76 This value , He is in the computer 01001100.
Use %d The consciousness of is “ Translate the given value into decimal text and print it out ”. therefore %d Convert it to characters 7 and 6 And it's shown as 76.
printf Print type
printf Symbols used to print different types of values , Using the wrong specifier will print unexpected results .printf("%c %d", '$', 2*a) printf What's printed is the value , Regardless of variables , Constant , Or expression .
Integer conversion
| Conversion instructions | Output |
|---|---|
| %d | Decimal display , Printable int and short, It can also be written %i |
| %ld | long |
| %lld | long long int |
| %o | octal int |
| %lo | octal long |
| %x | Hexadecimal int |
| %lx | Hexadecimal long |
| %#o | Octal prefix 0 Show |
| %#x | Hexadecimal prefix 0x Show |
| %#X | Hexadecimal prefix 0X Show |
| %u | unsigned int |
| %lu | unsigned long |
| %4d | Minimum field width , If there is not enough space for the number to be printed , Automatically expand |
| %zd | perhaps %zu, Express size_t Type value ,sizeof() perhaps strlen() The result type returned by the operation . In the early C use %ul To receive |
Floating point conversion
| Conversion instructions | Output |
|---|---|
| %f | Floating point decimal notation means float or double,float Will be automatically converted to double Output |
| %6.2f | Indicates the number to be printed ⾄ Less occupation 6 Character width , And ⼩ After the count ⾯ Yes 2 position ⼩ Count ,⼩ Counting points ⼀ position , So the integer part ⾄ Less occupation 3 position |
| %.2f | Indicates that the integer part is normal , The decimal part is reserved 2 He and %0.2f The meaning is the same |
| %Lf | long double This should be capitalized |
| %e | Use e Count to represent floating point numbers |
| %g | Automatically select according to the value %f perhaps %e |
Print floating point numbers of different precision
https://blog.csdn.net/dodamce/article/details/115297198
Other conversions
| Conversion instructions | Output |
|---|---|
| %c | Single character |
| %s | Character string |
| %p | Pointer value |
| %% | Print a percent sign |
| \n | Line break |
| \\ | Slash |
| \’ | Single quotation marks |
| \" | Double quotes |
Mark

#define PAGES 959
int main(void)
{
printf("*%d*\n", PAGES);
printf("*%2d*\n", PAGES);
printf("*%10d*\n", PAGES);// Print 10 Character width
printf("*%-10d*\n", PAGES);// Align left
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); // Retain 6 Decimal place
printf("*%e*\n", RENT); //e notation
printf("*%4.2f*\n", RENT); //4 Width ,2 Decimal place , If the width is not enough, the original number will be displayed
printf("*%3.1f*\n", RENT);
printf("*%10.3f*\n", RENT); //10 Width ,3 Decimal place
printf("*%-10.3f*\n", RENT); // Align left
printf("*%10.3E*\n", RENT);
printf("*%+4.2f*\n", RENT); // Display symbols
printf("*%010.2f*\n", RENT); // use 0 Not enough bits filled
return 0;
}
// *3852.990000*
// *3.852990e+03*
// *3852.99*
// *3853.0*
// * 3852.990*
// *3852.990 *
// * 3.853E+03*
// *+3852.99*
// *0003852.99*
Print string
There are three ways to print long string newlines ,
- The use of multiple printf sentence .
- Use backslash , But the second line must be left facing , Otherwise, there will be blank space
- ( recommend ) Use double quotation mark linker
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 function
C The language library has multiple input functions , however scanf Is the most common one , Because it can read data in different formats .
scanf and printf The way of use is basically the same , But be careful .scanf Functions use pointers to variables .
That is, pay attention to two techniques when using :
- scanf Read the value of the basic variable type before the variable name &
- Read a string into a character array , The variable name is not preceded by &
scanf Function USES blank ( A newline , Tabs and spaces ) Divide the input into multiple fields . That is, use white space as a separator .
scanf The conversion description used is the same as printf identical , See the table above . The only difference ,scanf All input floating-point types are applied to float On type .
give an example
scanf Read a %d, That is, read an integer :
- He will skip the blank characters entered , Read from the first non white space character
- Then read the numeric characters , Until you encounter non numeric characters , He thought he had reached the end of the whole number .
- Then put the number into the variable .
If you use %s Conversion instructions ,scanf Will read all characters except white space .
- He will skip the blank characters entered , Read from the first non white space character
- Read the characters in turn until you encounter whitespace again , stop it .
Other input functions
We see that if I want to read a string with spaces, use scanf Can't read .
Use getchar() and fgets() It is more suitable for dealing with some special cases of strings . I 'll talk about it later .
It's just scanf Is a more general input method .
Format input
scanf("%d,%d:, &n, &m);
Then the input must be in the following form
55,45
scanf("%d %d:, &n, &m);
Then the input must be in the following form
55 45
边栏推荐
- Cloud native architecture (04) -cncf
- 阿里云对象存储oss+picgo+typora实现构建图床
- Pkav simple blasting
- visdom的使用
- 今日睡眠质量记录80分
- When I was young, I thought my father was omnipotent
- 数学分析_笔记_第1章:集合与映射
- C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
- Ip6809 three coil 15W wireless charging transmitter scheme IC British chip
- jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
猜你喜欢

Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling

C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)

华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位

Build a gocd environment

Markdown add background color to the picture

SIFT feature point extraction

MCS: continuous random variable - student's t distribution

云原生大趋势下的容器化技术现状与发展

Differences between fs4059a and fs5080e charging chips

H5 adaptive full screen
随机推荐
read 文件一个字节实际会发生多大的磁盘IO?
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
What if win11 cannot record audio? Solution of win11 unable to input sound
今日睡眠质量记录80分
数据库连接异常:create connection error, url: jdbc:mysql://ip/数据库名, errorCode 0, state 08S01问题处理
知识点滴 - 折叠锻打和大马士革钢
mysql字符集
What does it mean to open more accounts? Why open more accounts? How to implement it safely?
Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents
Export PDF with watermark
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
STM32 clock tree misconfiguration causes boot to enter hard interrupt
【斯坦福计网CS144项目】Lab2: TCPReceiver
fastjson中的@JSONField注解
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
抽奖 ddd 代码
2022年中国重卡智能化升级专题研究
FS2119A同步升压IC输出3.3V和FS2119B同步升压IC输出5V
MySQL面试真题(二十六)——滴滴2020年笔试题
Go language -panic and recover