当前位置:网站首页>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
边栏推荐
- Mathematical analysis_ Notes_ Chapter 1: set and mapping
- Wechat applet: Star Trek spaceship ticket production and generation
- Common wireless charging and transmitting IC chips
- Win11应用商店下载的软件怎么移到桌面
- Go language - custom error
- View of MySQL introductory learning (III)
- Composite API
- Win11不能录制音频怎么办?Win11无法录入声音的解决方法
- 树莓派assert初步使用练习
- H5 adaptive full screen
猜你喜欢

Go language -panic and recover

Today's sleep quality record 80 points

H5 adaptive full screen

Wechat applet: future wife query generator

FS2119A同步升压IC输出3.3V和FS2119B同步升压IC输出5V

Wechat applet: a new interesting test

Lottery DDD code

软件设计开发笔记2:基于QT设计串口调试工具

制造业数字化转型存在问题及原因分析

51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
随机推荐
App automated test appium advanced
Differences between fs4059a and fs5080e charging chips
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
Implementation of pyGame music related functions
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
Jsonfield annotation in fastjson
QT QWidget nesting relative position acquisition (QT drawing nesting)
Introduction to MySQL (II) sub query + Association
[opencv450] image subtraction, binarization, threshold segmentation
jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
Today's sleep quality record 80 points
Introduction to JDBC (I) DML operation
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
STM32 clock tree misconfiguration causes boot to enter hard interrupt
高等数学(第七版)同济大学 习题1-8 个人解答
STC 32 Bit 8051 Single Chip Computer Development Example Tutorial one development environment
[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming
Konva series tutorial 1:what is konva?
Cloud native architecture (04) -cncf
Ip6809 three coil 15W wireless charging transmitter scheme IC British chip