当前位置:网站首页>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
边栏推荐
- visdom的使用
- MySQL Foundation
- Wechat applet: future wife query generator
- STC 32比特8051單片機開發實例教程 一 開發環境搭建
- How to move the software downloaded from win11 app store to the desktop
- MySQL面试真题(二十七)——RFM分析法对用户进行分类
- Jenkins installs and deploys and automatically builds and publishes jar applications
- fastjson中的@JSONField注解
- 英集芯ip6806无线充电方案5W过Qi认证外围精简14颗器件
- Today's sleep quality record 80 points
猜你喜欢

Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents

Go language -panic and recover

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

JDBC introductory learning (II) encapsulation tool class

H5 adaptive full screen

Spark 离线开发框架设计与实现

Management system of borrowed books based on SSM framework

SIFT feature point extraction
![[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale](/img/2d/96a370c90dcb7091038afad33bc4b4.png)
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale

Current situation and development of containerization technology under the cloud native trend
随机推荐
知识点滴 - 折叠锻打和大马士革钢
[opencv450] inter frame difference method
Webrtc[47] - a common way for webrtc to save YUV data
Leetcode 797: all possible paths
面对新的挑战,成为更好的自己--进击的技术er
Design and implementation of spark offline development framework
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
Implementation of MySQL custom sequence number
STM32 clock tree misconfiguration causes boot to enter hard interrupt
MySQL面试真题(三十)——贝壳-房产订单分析
visdom的使用
MySQL Foundation
手机无线充电双线圈15W方案SOC英集芯IP6809
MySQL面试真题(二十八)——案例-通讯运营商指标分析
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
Real MySQL interview question (30) -- shell real estate order analysis
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
MySQL面试真题(二十五)——常见的分组比较场景
IP6809三线圈15W无线充电发射端方案ic英集芯
Wechat applet: unfashionable love talk