Variables, types, and arithmetic · 变量、类型与算术
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
每个值都有类型
- 在 C 里,每个变量都有一个类型,说明它存放的是哪种值。
- 最常用的三种:
int(整数)、double(带小数点的数)、char(单个字符)。 - 创建变量时必须写出类型:
int n = 5;或double pi = 3.14;。
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
做算术
- C 用
+、-、*、/做运算,就像计算器一样。 *和/比+和-先算。用小括号( )可以改变顺序。%运算符给出除法的余数:17 % 5是2。
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
整数的除法不一样
- 两个
int相除时,C 会把小数部分丢掉:7 / 2是3,不是3.5。 - 想保留小数,至少有一边要是
double。7 / 2.0是3.5。 - 可以用
(double)把int强制转换成double:(double)sum / count。
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
读取输入
scanf把输入读进变量。scanf("%d", &n);读入一个int到n。- 注意
n前面的&:scanf需要变量的地址,才能把值填进去。 - 在这个练习场里,按 **Run(运行)**会先让你输入程序要读取的内容,每行一个;**Check answer(检查答案)**会替你喂入示例输入。
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
常见错误
- 声明变量要带类型:
int n;。 - 整数除法向下取整:
7 / 2是3,不是3.5。
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
现在轮到你了
- 整数运算用
/和%;需要小数时用double(或强制转换)。 - 函数任务里,不要自己写
main—— 检查器会提供。 - 按 **Run(运行)**编译并运行,然后 Check answer(检查答案)。
Variables hold a value · 变量保存一个值
A variable is a named box of a fixed type; assigning replaces it. · 变量是一个固定类型的带名字的盒子;赋值会替换它。
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · 用 scanf 读入一个整数 n。一个盒子装 6 件物品。精确打印两行:Full boxes: 加上 n / 6 的值,以及 Left over: 加上 n % 6 的值。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not · 不 write a main. · 完成 double average3(int a, int b, int c),让它以 double 返回三个数的平均值。小心整数除法 —— 要除以 3.0,而不是 3。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not · 不 write a main. · 编写 double hyp(double a, double b),返回斜边的长度:即 a*a + b*b 的平方根。使用 <math.h> 中的 sqrt。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。