《C和指標》學習筆記[第四章 語句]
阿新 • • 發佈:2022-05-31
4.13問題
1.
它是合法的,執行了加減乘除操作,但沒有賦值
2.
x = expression
3.
合法
4.
if 後面用;空語句,或者else的條件該到用if的, 用!就可以.
5.
0
1
2
3
4
5
6
7
8
9
6.
個人感覺都差不多。水平有限
7.
少了{},while的執行體就是第一個語句
正確的
while ((ch = getchar()) != EOF) { checksum += ch; putchar(ch); } printf("chechsum = %d\n", checksum);
8.
無論條件是否成立,都需要執行一次的環境下
9.
odd
even
odd
odd
even
odd
10.
int main(void) { int i; scanf("%d", &i); for (; i > 0; i--) { printf("\n"); } return EXIT_SUCCESS; }
11.
int main(void) { int x, y, a, b; scanf("%d %d %d %d", &x, &y,&a,&b);if (x < y || a >= b) { printf("WRONG\n"); } else printf("RIGHT\n"); return EXIT_SUCCESS; }
12.
int main(void) { int in_year; int leap_year; scanf("%d", &in_year); if ((in_year % 4 == 0 && in_year % 100 != 0) || in_year % 400 == 0) { leap_year = 1; } else leap_year = 0; printf("leap_year is %d\n", leap_year); return EXIT_SUCCESS; }
13.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" int main(void) { int in_num; while (scanf("%d", &in_num) != 0) { switch (in_num) { case 1: printf("who\n"); break; case 2: printf("waht\n"); break; case 3: printf("when\n"); break; case 4: printf("where\n"); break; case 5: printf("why\n"); break; default: printf("don't know"); break; } } return EXIT_SUCCESS; }
14.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" static int full_num = 10; void eat_hamberger(void) { printf("eat one hamberger\n"); full_num--; } int hungry(void) { return full_num; } int main(void) { while (hungry()) { eat_hamberger(); } return EXIT_SUCCESS; }
15.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" static int full_num = 0; void eat_hamberger(void) { printf("eat one hamberger\n"); full_num--; } int hungry(void) { if (full_num > 0) return 1; else return 0; } int main(void) { do { eat_hamberger(); } while (hungry()); return EXIT_SUCCESS; }
16.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" int main(void) { int precipitsting; int temperature; scanf("%d%d", &precipitsting, &temperature); if (precipitsting == 1) { if (temperature < 32) printf("snowing\n"); else printf("raining\n"); } else { if (temperature < 60) { printf("cold\n"); } else printf("warm\n"); } return EXIT_SUCCESS; }