变量、常量、scanf、赋值、运算
1. C语言必须对变量进行声明类型,可以不初始化
int a = 0; 声明后,类型不能改变
int const NUM = 100; 常量,定义后值不能改变
2. scanf()
&不能丢
#include <stdio.h> int main() { int a; int b; printf("输入两个整数:"); scanf("%d %d", &a, &b); printf("%d + %d = %d\n", a, b, a+b); return 0; }
3.浮点数 单精度float、双精度double
整数/整数 得到的结果为 丢弃小数部分的 整数部分 如10/3 = 3
整数/浮点数 或 浮点数/整数 会先把整数变为浮点数然后两个浮点数进行运算得到浮点数
#include <stdio.h> int main() { printf("%d / %d = %d\n", 10, 3, 10/3); return 0; } // 10 / 3 = 3 #include <stdio.h> int main() { printf("%d / %d = %f\n", 10, 3, 10/3); return 0; } // 10 / 3 = 0.000000 #include <stdio.h> int main() { printf("%d / %d = %f\n", 10, 3, 10.0/3); return 0; } // 10 / 3 = 3.333333
4.printf()中float和double都用 %f;scanf()中float用 %f,double用 %lf
#include <stdio.h> int main() { double a; double b; printf("输入被除数与除数:"); scanf("%lf %lf", &a, &b); //printf()中float和double都用 %f;scanf()中float用 %f,double用 %lf printf("%f / %f = %f\n", a, b, a/b); return 0; }

浙公网安备 33010602011771号