《c程序设计语言》第2章 类型、运算符与表达式
时间:2010-06-12 来源:oychw
2.1 变量名
2.2 数据类型及长度
基本数据类型:char,int,float,double
限定词:short,long,signed,unsigned
相关的定义参见:<limits.h> and <float.h>,以及附录B.
常见类型的字节占用数:
#include <stdio.h>
main()
{
printf("the bytes of char:%d\n",sizeof(char));
printf("the bytes of int:%d\n",sizeof(int));
printf("the bytes of short int:%d\n",sizeof(short int));
printf("the bytes of long int:%d\n",sizeof(long int));
printf("the bytes of float:%d\n",sizeof(float));
printf("the bytes of double:%d\n",sizeof(double));
printf("the bytes of long double:%d\n",sizeof(long double));
}
windows XP 32位下:
the bytes of char:1
the bytes of int:4
the bytes of short int:2
the bytes of long int:4
the bytes of float:4
the bytes of double:8
the bytes of long double:12
SUSE Linux 10 32位下运行:
$ ./sizeof_type.exe
the bytes of char:1
the bytes of int:4
the bytes of short int:2
the bytes of long int:4
the bytes of float:4
the bytes of double:8
the bytes of long double:12
SUSE Linux 10 64位下运行:
./sizeof_type.exe
the bytes of char:1
the bytes of int:4
the bytes of short int:2
the bytes of long int:8
the bytes of float:4
the bytes of double:8
the bytes of long double:16
2.3 常量
转义字符列表:
\a alert (bell) character \\ backslash
\b backspace \? question mark
\f formfeed \' single quote
\n newline \" double quote
\r carriage return \ooo octal number
\t horizontal tab \xhh hexadecimal number
\v vertical tab
常量表达式是仅仅包含常量的表达式。在编译时求值,而不是在运行时求值。
字符串常量
枚举常量
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
/* FEB = 2, MAR = 3, etc. */
枚举跟#define相比,常量值可以自动生成。
Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you. Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration. Nevertheless, enumeration variables offer the chance of checking and so are often better than #defines. In addition, a debugger may be able to print values of enumeration variables in their symbolic form.
2.4 声明
Const限定预算不能修改。比如:
const double e = 2.71828182845905;
const char msg[] = "warning: ";
2.5 算术运算符
+, -, *, /,%
从左至右结合,低优先级:+, -,高优先级:*, /,%。一元的运算符+, -最高。
2.6 关系运算符与逻辑运算符
关系运算符:> >= < <=
相等性运算符:== !=,优先级低于关系运算符。关系运算符优先级低于算术运算符。
逻辑运算符&&优先级高于||,但是低于关系和相等性运算符。
2.7 类型转换
Ctype.h定义了一组与字符集无关的测试和转换函数。比如tolower,isdigit,更多信息参见附录B
类型转换规则见附录A.6,注意float不会自动转换为double类型。使用float可以节省存储空间和机器执行时间。
无论是否进行符号扩展,字符型变量都转换为整型变量。
强制类型转换和1元运算符优先级一样。
2.8 自增运算符与自减运算符
2.9 按位运算符
& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< left shift
>> right shift
~ one's complement (unary)
2.10 赋值运算符与表达式
2.11 条件表达式
z = (a > b) ? a : b; /* z = max(a, b) */,为了提高可读性,建议条件判断部分使用括号。
2.12 运算符优先级与求知次序
Operators Associativity
() [] -> . left to right
! ~ ++ -- + - * (type) sizeof right to left
* / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& left to right
^ left to right
| left to right
&& left to right
|| left to right
?: right to left
= += -= *= /= %= &= ^= |= <<= >>= right to left
, left to right
注意位运算符的优先级低于等于和不等判断,
if ((x & MASK) == 0)
同一运算符的多个操作数的计算顺序没有规定:
x = f() + g();
函数参数求值的顺序也没有规定。其他可能发生问题的有:
a[i] = i++;