在程序码中代表运算的符号,参与运算的数值或变数称为运算元(Operand)
代表平常使用的数学运算,加、减、乘、除分别是 +
, -
, *
, /
运算的结果可以用格式化字串的方式显示
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("%d", 8+9);
return 0;
}
运算遵守数学中先乘除後加减,括号先算的原则
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("%d", 6+7*8-9);
return 0;
}
乘法遇到括号时 *
不可以省略
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("%d", (6+7)*(8+9));
return 0;
}
运算的结果可以用变数储存,变数也可以当作运算元
#include<stdio.h>
#include<stdlib.h>
int main(){
int x=0, y=10;
printf("%d\n", x);
x=8+9;
printf("%d %d\n", x, x+y);
return 0;
}
运算结果的型别会和运算元相同,若左、右是整数,不管做什麽运算都是整数
下面的例子中,因为 5
, 2
是整数,所以不管用 int
型态的 a
存还是用 float
型态的 b
存都会得到 2(不过 b
显示的内容会有小数点)
#include<stdio.h>
#include<stdlib.h>
int main(){
int a;
float b;
a = 5/2;
b = 5/2;
printf("%d %f\n", a, b);
return 0;
}
如果运算元中有一或以上个浮点数,则结果是浮点数,但用 int
型的变数储存仍然是整数
#include<stdio.h>
#include<stdlib.h>
int main(){
int a;
float b;
a = 5.0/2;
b = 5.0/2;
printf("%d %f\n", a, b);
a = 5/2.0;
b = 5/2.0;
printf("%d %f\n", a, b);
a = 5.0/2.0;
b = 5.0/2.0;
printf("%d %f\n", a, b);
return 0;
}
程序设计中的等号 =
代表的是指派,而不是两者相等
执行时会先把等号右边的值算出来,再把左边变数的值设为右边算出来的结果,所以以下的内容在程序设计中是符合语法的
#include<stdio.h>
#include<stdlib.h>
int main(){
int x=0;
printf("%d\n", x);
x=x+1;
printf("%d\n", x);
x=x+1;
printf("%d\n", x);
x=x+1;
printf("%d\n", x);
return 0;
}
因为 =
是算出执行的当下右边的值後指派给左边,当左右两边都是变数时,两者的关系并不会绑定在一起
#include<stdio.h>
#include<stdlib.h>
int main(){
int x=0, y=10;
printf("x= %d y= %d\n", x, y);
x=y*2;
printf("x= %d y= %d\n", x, y);
y=20;
printf("x= %d y= %d\n", x, y);
return 0;
}
上面的例子中,执行到第7行时 x
的值被设为现在 y*2
的结果,也就是 20 这个数而不是 y*2
这个式子,所以在第9行即使 y
被设为 20
,x
也不会变成 20*2
也就是说,变数虽然可变,但没有人去动它,它就不会变
数学运算之一,优先顺序和乘、除相同,结果为两数相除的余数,符号是 %
透过除模可以知道某数是不是另一个数的倍数:当除模的结果为0,代表两可以整除,所以两者是倍数关系
或是用於限制数值的范围: 当某数除模 n 时,结果只会有 n 种(0, 1, ...,~n-1)
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("%d\n", 0%5);
printf("%d\n", 1%5);
printf("%d\n", 2%5);
printf("%d\n", 3%5);
printf("%d\n", 4%5);
printf("%d\n", 5%5);
printf("%d\n", 6%5);
return 0;
}
让视窗变成可打字的状态,按下 enter 後结束,并把输入的内容储存於变数中的函式
scanf()
的参数分为两个部分,代表输入格式的字串,以及用来储存资料的变数指标
输入的格式用格式化字串的预留位置表示,输入格式决定这个 scanf()
可以输入几个数值、输入数值的型别以及对应的顺序
如果要输入的数值只有一个,且为整数型态
"%d"
如果是字元或是浮点数要用 %c
和 %f
"%c"
"%f"
如果要在同一行中输入多个数值,输入时每个数值间用空格隔开
"%d%d%d"
也可以多个型别混用,且要注意格式顺序,例如: 输入 3 个数值,依序是整数、字元、整数
"%d%c%d"
字串中占位符号间的空格可有可无
"%d %d"
"%d%d"
以上两种写法的结果都相同(但有些认证或考试的系统有规定不能空格,要多加注意)
变数的指标代表的是变数在记忆体中的位置,可以在变数名称前面加上 &
取得
例如有个变数的名称是 x
,那 x
的指标就是 &x
更多关於指标的内容会再後面的文章介绍(挖坑+1)
输入一个整数,并储存在变数 x
中
#include<stdio.h>
#include<stdlib.h>
int main(){
int x;
scanf("%d", &x);
printf("The value of x is %d.\n", x);
return 0;
}
输入多个变数,结合运算子可以做到一些简单的功能
#include<stdio.h>
#include<stdlib.h>
int main(){
float x, y;
scanf("%f%f", &x, &y);
printf("%f × %f = %f.\n", x, y, x*y);
return 0;
}
挖坑清单:
main()
前面的 int
,和最後一行的 return 0;
\0
在字串、字元中的意思
>>: 30天学会 Python: Day 4-《资料Collection》
SEO优化-内部连结和外部反向连结 所谓的连结通常是使用文字、图片或是网址作为超连结,SEO优化中常...
加入主画面 (Add to Home screen) 加入主画面 (Add to Home scre...
Today I will introduce the order of compiling. Fir...
这题是graph的问题。 Input 这题是大学修课挡修的问题,我是没有遇过挡修啦,所以没什麽感觉,...
图片来源 其实心中想要考取商业分析师(PBA - Professional in Business...