Notes
-
C provides two major categories of data types: integer types and floating-point types
-
The getchar() function reads the next input character
-
Constants can use numbers with decimal points
-
In printf(), %f is used to handle floating-point values, and %.2f specifies that the output floating-point number displays only two digits after the decimal point.
-
The data-type keywords in C are as follows:
| K&R keywords | Keywords added by the C90 standard | Keywords added by the C99 standard |
|---|---|---|
| int | signed | _Bool |
| long | void | _Complex |
| short | _Imaginary | |
| unsigned | ||
| char | ||
| float | ||
| double |
-
The int keyword denotes the basic integer type, while long, short, unsigned, and signed provide variations of the integer type
-
E notation: for example, 3.16E7 represents 3.16×10^7
-
ISO Cspecifies that the minimum range ofintis-32768~32767
int dogs, cats = 94;
/* 有效,但是这种格式很糟糕
不要把初始化的变量和未初始化的变量放在同一条声明中!
*/
-
In
printf("%d", var);, %d is called a conversion specification. It specifies the formatprintf()should use to display a value. -
Use %d to display a number in decimal; use %o to display a number in octal; and use %x to display a number in hexadecimal. In addition, to display the prefixes 0, 0x, and 0X for numbers in the respective bases, you must use %#o, %#x, and %#X, respectively.
-
int, short (short for short int), long (short for long int), and long long (short for long long int) are all signed types
-
Adding the keyword
signedbefore any signed type emphasizes the intention to use a signed type. -
The most common configuration on personal computers today is:
long longoccupies 64 bits,longoccupies 32 bits,shortoccupies 16 bits, andintoccupies 16 or 32 bits. -
To treat a smaller constant as the
longtype, add an l or L suffix to the end of the value. If the system supports thelong longtype, thellorLLsuffix can be used to denote a value of thelong longtype. -
The
printf()function uses%uto display values of theunsigned inttype, and printing values of thelongtype uses the%ldconversion specification. If int and long are the same size on the system, %d can be used. %lx means printing a long integer in hexadecimal format, while %lo means printing a long integer in octal format. %hd means displaying a short integer in decimal, and %ho means displaying a short integer in octal. The h and l prefixes can both be used together with u to represent unsigned short and unsigned long types.
# 有无符号类型总结
1.有符号类型:short,int,long,long long
2.无符号类型:unsigned short,unsigned int(简称unsigned),unsigned long,unsigned long long
# 转换说明符总结
%d 表示以十进制显示int类型整数(int类型就是有符号类型)
%ld 表示以十进制显示long类型整数(long类型是有符号类型)
%hd 表示以十进制显示short类型整数(short类型是有符号类型)
%u 表示以十进制显示unsigned int类型整数(unsigned int 或简称 unsigned,其为无符号类型)
%o 表示以八进制显示int类型整数
%x 表示以十六进制显示int类型整数
%lx 表示以十六进制显示long int(简称long)类型的整数
%ho 表示以八进制显示short int(简称short)类型的整数
%lo 表示以八进制显示long int(简称long)类型的整数
%lu 表示以十进制显示unsigned long int类型的整数
%lld 表示以十进制显示long long类型的整数
%llu 表示以十进制显示unsigned long long类型的整数
-
Note: When using the
printf()function, be sure to check whether the value to be printed matches the type of the conversion specification. -
The char type is used to store characters. During initialization, a single character is enclosed in single quotation marks (a single character enclosed in single quotation marks is called a character constant).
# 但是字符实际是以数值形式存储的,所以也可以用数字代码值来给char类型变量赋值
char grade = 65;
# 但是最好是使用字符常量,不是数字代码值,因为在ASCII码中,65对应字母'A',但是前提是系统使用ASCII码。
-
For a variable of the char type, the conversion specifier is %c; however, I found that the %c conversion specifier can also be used to print an int variable, in which case the character printed is the one corresponding to the int variable's value. Yes, the conversion specifier determines how the data is displayed, not how the data is stored.
-
The char type is implemented as a signed type in some compilers (so its representable range is -128 ~ 127) and as an unsigned type in other compilers (with a representable range of 0 ~ 255).
-
By default, the compiler treats a floating-point constant as a constant of the
doubletype. Adding theforFsuffix after a floating-point number overrides the default. Using thelorLsuffix makes the number along doubletype. -
The
clanguage has three floating-point variable types:float,double, andlong double. -
The
printf()function uses the%fconversion specification to print floating-point numbers of thefloatanddoubletypes, uses%eto print floating-point numbers in exponential notation, and printing thelong doubletype requires the%LF,%Le, or%Laconversion specification. -
sizeofis a built-in operator in theclanguage that gives the size of a specified type in bytes.c99andc11provide the%zdconversion specification to match the return type ofsizeof. Some compilers that do not supportc99andc11can use%uor%luinstead of%zd.