#include <stdio.h>
int main(void) {
// this is a comment
/* This is a multi - line comment */
printf("Hello World!");
printf("\nC rocks!");
return 0;
}
2、编写一个程序,分别声明并初始化四个类型为char、int、float和double的变量。
int main(void) {
char c = 'a';
int x = 123;
float f = 123.456f;
double d = 789.101112;
}
3、编写一个程序,分别初始化并打印四个类型为字符型、整型、浮点型和双精度浮点型的变量。
#include <stdio.h>
int main(void) {
char c = 'a';
int x = 123;
float f = 123.456f;
double d = 789.123;
printf("The values are: %c, %d, %.3f, %.3f\n", c, x, f, d);
return 0;
}
4、编写一个程序,初始化两个整数。声明一个第三个整数变量,该变量表示前两个整数的和。输出结果。
#include <stdio.h>
int main(void) {
int x = 123;
int y = 456;
int z = x + y;
printf("The result is: %d\n", z);
return 0;
}
5、编写一个执行整数除法的程序。
#include <stdio.h>
int main(void) {
int x = 9;
int y = 2;
int z = x / y;
printf("The result is: %d\n", z);
return 0;
}
#include <stdio.h>
int main(void) {
int x = 9;
int y = 2;
double z = (double)x / y;
printf("The result is: %.3f\n", z);
return 0;
}
7、编写一个程序,检查两个整数变量的值是否相同。
#include <stdio.h>
int main(void) {
int x = 10;
int y = 20;
if (x == y) {
printf("The values are equal.\n");
} else {
printf("The values are not equal.\n");
}
return 0;
}
8、编写一个程序,检查一个整数变量是否大于 50 且小于 100。
#include <stdio.h>
int main(void) {
int x = 75;
if (x > 50 && x < 100) {
printf("The value is greater than 50 and less than 100.\n");
} else {
printf("The value is not within the (50..100) range.\n");
}
return 0;
}