Skip to content

2 、变量与运算符


一、变量🧀

Example

e.g. 需求:有地方放数字;有办法输入数字;输入的数字能进行运算

#include <stdio.h>

int main() {
    int price = 0;
    printf("Enter Price:");
    scanf("%d", &price);

    int change = 100 - price;
    printf("Change Price:%d", change);
    return 0;
}

1、变量定义🧀

\[ <类型名称><变量名称> \]
Danger

不能数字开头

保留字

1
2
3
4
5
6
7
auto, break, case, char, const,
continue, default, do, double,
else, enum, extern, float, for,
goto, if, int, long, register, return,
short, signed, sizeof, static,
struct, switch, typedef, union,
unsigned, void, volatile, while

2、变量初始化、赋值🧀

#include <stdio.h>

int main() {
    int price = 0;
    printf("Enter Price:");
    scanf("%d", &price);

    int change = 100 - price;
    printf("Change Price:%d", change);
    return 0;
}

// 在C99标准下可以随处定义变量, ANSI-C则不行
scanf("%d", &price);
// 读入下一个整数赋值给price

Note

a=bb=a 是不同的赋值语句

所有变量都要先定义初始值

1
2
3
4
5
6
7
8
int main() {
    int i = 0;
    int j = 0;
    j = i + 10;
    printf("%d j: \n", j);

    return 0;
}

3、变量输入🧀

#include <stdio.h>

int main() {
    int a = 0;
    int b = 0;

    scanf("hello%d %d", &a, &b);
    printf("%d %d\n", a, b);

    return 0;
}
// 出现在 scanf 中的部分必须在输入时输入,否则会出错

4、常量🧀

#include <stdio.h>

int main() {
    int price = 0;
    const int AMOUNT = 100;
    printf("Enter Price:");
    scanf("%d", &price);

    int change = AMOUNT - price;
    printf("Change Price:%d", change);
    return 0;
}
// const是修饰符,在int前面,且一旦初始化,不能修改

try:让用户可以输入AMOUNT怎么办?

#include <stdio.h>

int main() {
    int price = 0;
    int amount = 0;
    printf("Enter Price:");
    scanf("%d", &price);

    printf("Enter Amount:");
    scanf("%d", &amount);

    int change = amount - price;
    printf("Change Price:%d", change);
    return 0;
}

5、浮点数🧀

英寸换算
  • 美国使用英制计量单位,习惯用几尺几寸(英尺英寸)来报自己的身高。如果遇到一个美国人告诉你他是5英尺7,他的身高应该是一米几呢?→(5+7÷12)×0.3048=1.7018米
int main() {
    int foot = 0;
    int inch = 0;
    printf("Please enter the foot:");
    scanf("%d", &foot);

    printf("PLease enter the inch:");
    scanf("%d", &inch);

    printf("The height is:%d\n", (foot + inch / 12) * 0.3048);

    return 0;
}
// 有问题因为类型是int

// 改正如下
int main() {
    double foot;
    double inch;
    printf("Please enter the foot:");
    scanf("%lf", &foot);

    printf("PLease enter the inch:");
    scanf("%lf", &inch);

    printf("The height is:%f\n", (foot + inch / 12) * 0.3048);

    return 0;
}
// 输入要改成 scanf("%lf",……)
// 打印要改成 printf("%f")

Tip

整数 浮点数
int double/float
printf("%d",...) printf("%f",...)
scanf("%d",...) scanf("&lf",...)

二、表达式与运算符🧀

1、表达式🧀

运算符、算子

计算时间差
// 算成分钟
int main() {
    int hour1;
    int minute1;
    int hour2;
    int minute2;

    printf("Please enter the first hour and minute:");
    scanf("%d %d", &hour1, &minute1);

    printf("Please enter the second hour and minute:");
    scanf("%d %d", &hour2, &minute2);

    printf("%d", (hour2 - hour1) * 60 + (minute2 - minute1));

    return 0;
}
// 算成小时
int main() {
    int hour1;
    int minute1;
    int hour2;
    int minute2;

    printf("Please enter the first hour and minute:");
    scanf("%d %d", &hour1, &minute1);

    printf("Please enter the second hour and minute:");
    scanf("%d %d", &hour2, &minute2);

    printf("%f", (hour2 - hour1) + (minute2 - minute1) / 60);

    return 0;
}

2、运算符优先级🧀

求平均值
1
2
3
4
5
6
7
8
9
int main() {
    int a;
    int b;
    printf("Enter two integers:");
    scanf("%d %d", &a, &b);
    double c = (a + b) / 2.0;
    printf("%f\n", c);
    return 0;
}

优先级表格

单目:参与一次运算的算子数

Info

单目和赋值是自右向左

优先级 运算符 运算 结合关系 举例
1 + 单目不变 自右向左 a*+b
1 - 单目取负 自右向左 a*-b
2 * 自左向右 a*b
2 / 自左向右 a/b
2 % 取余 自左向右 a%b
3 + 自左向右 a+b
3 - 自左向右 a-b
4 = 赋值 自右向左 a=b

Note

赋值也是运算,也有结果

a = b = 6 > a = (b = 6)

Warning

“嵌入式赋值”不要用

1
2
3
int a;
int b;
int c =1 + (b=a)

3、交换变量🧀

int main() {
    int a=5;
    int b=6;
    int c=0;
    c=b;
    b=a;
    a=c;
    printf("%d %d\n",a,b);

    return 0;

}
//用断点调试可以看过程

4、复合赋值🧀

+=-=,*=,/=,%=

递增/递减运算符(++,--)🧀

  count++
count=count+1

前缀/后缀🧀

表达式 运算 表达式的值
count++ 给count加1 count原来的值
++count 给count加1 count+1以后的值
count-- 给count减1 count原来的值
--count 给count减1 count-1以后的值
int main() {
    int a;
    a=10;
    printf("a++ = %d\n",a++);
    printf("a = %d\n",a);

    ++a;
    printf("++a = %d\n",a);
    printf("a = %d\n",a);

    return 0;
}

/*
a++ = 10
a = 11
++a = 12
a = 12
*/