C语言第二章--编程初步

变量

整型变量

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main()
{
int brothers, brides; // 声明整型变量
brothers = 7; // 初始化
brides = 7;
printf("%d brides for %d brothers.\n", brides, brothers); // 括号内用逗号分开三个参数,前一参数控制后两个参数以什么方式显示,即它指定了输出数据的格式,%后面的d表示变量会以十进制被显示出来,\n是换行符
return 0; // 返回控制权给操作系统
}

变量命名

一个变量由n个字母、数字和下划线组成,不能以数字开头,所以8_BallHash!Mary LouMary-Lou这类就属错误命名。一般以下划线开头的变量名常用在头文件中,为免和标准库冲突,不建议下划线开关命名的方式。

PS:变量名区分大小写。

算术语句

一元运算符

1
2
int expenditure = 75;
pringf("Your balance has changed by %d.", -expenditure); // expenditure的值并未改变,只是这里的%d被-75代替了。-不是一个动作,不需要执行指令,只是通知编译器在程序里创建一个负的常量

二元运算符

在赋值运算中,先计算=右边的表达式,然后赋值给左边的变量。

除法运算符总是得到整数值,如:45/7=6。若操作数不同号,结果为负,如:-45/745/-7都等于-6。若同号刚为正数。

取模运算符的结果总与左操作数符号一致,如:-45%745/-7分别是-33。还有几例:4%-7=4-4%7=-4-5%2=-15%-2=13%5=3-3%-5=-3

变量与内存

定点数

有符号整型变量

计算机的内存组织单位是字节,每个变量都会占据一定数量的内在字节,一个字节有8位,能存储-128~+127的整数。

  • signed char占用一个字节
  • short int占用两个字节
  • int占用四个字节
  • long int占用四个字节
  • long long int占用八个字节

short,long,long long可以用作short int, long int, long long int的缩写。

无符号整型变量

在有符号变量名前加unsigned,数值为非负,正值大一倍。如:

1
2
unsigned short count = 60000;   
unsigned long inchesPerMile =63360UL; // 数值后面的UL代表unsigned long

整型常量中以0开关的会被认为是八进制数,如:014等于十进制的12;

而以0x或0X开关的是十六进制数,如:0xa等于十进制的10,0xb=11,依此类推0xf=15。

浮点数

浮点数变量
1
2
float Radius = 2.5f; // 数值后同样需要标识
double Biggest = 123e30; // 默认是double型

任何数,只要有小数点,就是double类型,除非加了f,使它变成float类型。

当用E或e指定指数值时,这个常量不需要包含小数点。如:1E3f是flaot,3e8是double。

使用浮点数完成除法运算
1
#include <stdio.h>

define A 10.0f //define里的标识必须大写,在此程序中出现的常量A都由10.0f直接替换,这种替换发生在程序编译之前,当程序开始编译时,就不再有A这个常量而是10.0f,但源程序并没有改变,依然含有A。

1
2
3
4
5
6
7
8
9
10
int main()
{
float b,
c;
printf("Input a num:\n");
scanf("%f", &b); //stdio.h里的一个函数,表示从键盘读入字符,将之存入b中,b前加寻址运算符&。
c=A/b;
printf("%-8.2f / %.0f = %.2f", A, b, c); //%.2f里的-代表向左对齐,默认没有此号是右对齐,8代表此值占用8个字符位,.2表示小数点后有两位有效值,f是float。
return 0;
}

上面的define中定义的常量,也可以用下面代码完成:

1
const float A = 10.0f;  //A是变量,但值不能被改变


1
2
3
float c;
c=a/b;
printf("%-8.2f / %.0f = %.2f", a, b, c);

也可以写到一行里,像这样:

1
printf("%-8.2f / %.0f = %.2f", a, b, a/b);  //a/b将创建一个temp变量,当语句编译完后该temp变量自动销毁。

printf和scanf里第一个参数中常使用的变量格式:

1
2
3
4
5
6
7
8
9
%hd -> short  
%d -> int
%x -> 16进制数值
%c -> char
%ld -> long
%lld -> long long
%f or %e -> float
%lf or le -> double
%Lf or Le -> long double

常量

sizeof运算符

sizeof可以确定给定的类型占据多少字节。

1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
printf("%d",sizeof(int)); //结果是4,即int占用了4byte。sizeof的值类型是size_t类型的整数。
printf("%d", sizeof(100LL)); //8
}

size_t类型在<stddef.h>中定义,对应于一个基本整数类型

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>

int main()
{
size_t size = sizeof(long long);
printf("%d",size); //8
}

3 类型转换

隐式类型转换:

1
long double > double > float > long > int

无符号整数类型的级别从低到高:

1
signed char, short, int, long, long long

当两个操作数进行四则运算时,数值域自动从操作数中低的域转换到高的域,如:
int/doubleint为4byte整型,double为8byte有小数的浮点型 ,则运算前自动将int转换成double数值进行处理。

赋值语句可能造成数据丢失,如:

1
2
3
int num=3;
float a=11.3287f;
num = a; //num等于11,a在传值过程中的小数丢失了。

数值类型

字符类型

因为char类型只占用一个字节,所以如果表示成无符号数,则能表示0~255之间的整数。

1
2
3
4
5
6
7
8
#include <stdio.h>

int main()
{
char var='a'; //a在ASCII码值是97
printf("%d", var); //97
return 0;
}

反过来:

1
2
char var=97;
printf("%c", var); //a

由于char变量具有这种双重性,所以:

1
2
3
4
char var='a';   //97
int var1=var+3; //100
printf("%d\n", var1); //d
printf("The character is %c and the code value is %d.", var1, var1);

点击查看完整ASCII码

4.2 宽字符类型

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stddef.h> //引入宽字符类型

int main()
{
wchar_t var=L'a'; //定义宽字符类型
printf("%lc\n", var);
return 0;
}

4.3 枚举类型

一般情况:

1
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

直接在后面指定变量:

1
2
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} today=Wednesday, tomorrow=today+1;
printf("%d\n", today); //2

定义枚举量时,默认从0开始,Monday对应0,Sunday对应6,如果定义today is Wednesday,则today+1=Thursday。也可以指定Monday=1。这种情况对应下面这样:

1
enum FaceValue {two=2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};

4.4 赋值操作

一般来说:

1
num=nub+3;

可以简写成:

1
num+=3;

其中+可以换成-,*,/,%等。

4.5 数学函数

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <math.h>

int main()
{
double x=2.23;
printf("%lf\n", sqrt(x)); //1.493318
return 0;
}

其它函数:

1
2
3
4
5
6
7
8
9
10
floor(x) -> 返回不大于x的最大整数  
ceil(x) -> 返回不小于x的最小整数
fabs(x) -> x的绝对值
log(x)
log10(x)
exp(x)
pow(x)
sin(x)
cos(x)
tan(x)

5 练习

计算两件商品的总价值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
float item1price, item2price;
int item1num, item2num;

printf("Input the price of item1:\n");
scanf("%f", &item1price);
printf("Input the num of item1:\n");
scanf("%d", &item1num);

printf("Input the price of item2:\n");
scanf("%f", &item2price);
printf("Input the num of item2:\n");
scanf("%d", &item2num);

printf("The total value is %f", item1price*item1num + item2price*item2num);
}