第1章序論
1.1はじめに
#include <stdio.h>
int main() {
printf("hello, world\n");
}
C言語プログラム、どんなに小さなは、関数や変数で構成されています
- 関数が実行されるために、指定された計算操作文の数が含まれています
- 算出した保存用の設定変数の値
1.2変数と演算式は
、コメント、文、変数、演算式、環式、フォーマットされた出力
#include <stdio.h>
int main() {
int fahr, celsius;
int lower, upper, step;
lower = 0; /* 温度表的下限 */
upper = 300; /* 温度表的上限 */
step = 20; /* 步长 */
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
- int型の整数
- フロートフロート
- char型の文字(1バイト)
- 短い短整数
- 長い長い整数
- ダブル倍精度浮動小数点
for文1.3
#include <stdio.h>
int main() {
int fahr;
for (fahr = 0 ; fahr <= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
1.4シンボリック定数
#define 名字 替换文本
#include <stdio.h>
#define LOWER 0 /* 表的下限 */
#define UPPER 300 /* 表的下限 */
#define STEP 20 /* 步长 */
int main() {
int fahr;
for(fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
1.5文字の入力/出力
- 一度、文字GETCHARを読むのputchar
1.5.1ファイルのコピーを
#include <stdio.h>
#include <stdio.h>
int main() {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
1.5.2文字カウント
#include <stdio.h>
#include <stdio.h>
int main() {
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
1.5.3行数
#include <stdio.h>
int main() {
int c, nl;
nl = 0;
while((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}