Back home
中文
H7 / SECURITY RESEARCH NOTES

Chapter 3: Data and C

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 keywordsKeywords added by the C90 standardKeywords added by the C99 standard
intsigned_Bool
longvoid_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 C specifies that the minimum range of int is -32768 ~ 32767

int dogs, cats = 94;
/* 有效,但是这种格式很糟糕 
不要把初始化的变量和未初始化的变量放在同一条声明中!
*/
  • In printf("%d", var);, %d is called a conversion specification. It specifies the format printf() 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 signed before any signed type emphasizes the intention to use a signed type.

  • The most common configuration on personal computers today is: long long occupies 64 bits, long occupies 32 bits, short occupies 16 bits, and int occupies 16 or 32 bits.

  • To treat a smaller constant as the long type, add an l or L suffix to the end of the value. If the system supports the long long type, the ll or LL suffix can be used to denote a value of the long long type.

  • The printf() function uses %u to display values of the unsigned int type, and printing values of the long type uses the %ld conversion 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 double type. Adding the f or F suffix after a floating-point number overrides the default. Using the l or L suffix makes the number a long double type.

  • The c language has three floating-point variable types: float, double, and long double.

  • The printf() function uses the %f conversion specification to print floating-point numbers of the float and double types, uses %e to print floating-point numbers in exponential notation, and printing the long double type requires the %LF, %Le, or %La conversion specification.

  • sizeof is a built-in operator in the c language that gives the size of a specified type in bytes. c99 and c11 provide the %zd conversion specification to match the return type of sizeof. Some compilers that do not support c99 and c11 can use %u or %lu instead of %zd.