最新文章请访问:https://shizhz.me/
本文是对《K&R》第二章 “Types, Operators, and Expressions” 的一个摘要。
开篇第一段,原样摘抄下来:
Variables andconstants are the basic data objects manipulated in a program.Declarations list the variables to be used, and state what type they have and perhaps what their initial values are.Operators specify what is to be done to them.Expressions combine variables and constants to produce new values. Thetype of an object determines the set of values it can have and what operations can be performed on it.
变量名:
1、变量名由字母(a-zA-Z)、数字(0-9)、与下划线(_)组成,头字母不能是数字;
2、变量名区分大小写;以下语句声明了两个不同的变量:
int x, X;
3、关键字例如if, else, switch等不能用于变量名;
4、变量长度:最好小于31个字符,长度限制取决于编译器,太长也不具备实际意义了。
数据类型:
1、char: 单字节,表示本地字符集中的一个字符,本质上说其值是该字符对应的整数值,所以char类型可直接用于算术运算;
2、int: 整数,short int: 简写为short, long int: 简写为long,可用sizeof操作符判断字节大小;
3、float:单精度浮点数;
4、double:双精度浮点数, long double:扩展精度浮点数;
5、修饰符signed与unsigned可用于修饰char及任何整型,区别是signed表示符号位;
#include <stdio.h>
int main (int argc, char const* argv[])
{
printf("size of char: %d, short: %d, int: %d, long: %d, float: %d, double: %d, long double: %d\n", sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(float), sizeof(double), sizeof(long double));
return 0;
}
本机结果: