1.1 入门
#include <stdio.h>
int main() {
printf("Hello, World!\n");
}
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("%3d %6d\n", fahr, celsius);
fahr = fahr + step;
}
}
各数据类型的大小取决于具体机器.
- int
- float
- char
- short
- long
- double
- 数组
- 结构
- 联合
- 指针
- 函数
使用 float:
- 如果表达式中有 float 和 int, 那么会将 int 转为 float
#include <stdio.h>
int main() {
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
1.3 for 语句
#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 符号常量
#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 字符输入/输出
c = getchar()
putchar(c)
1.5.1 文件复制
#include <stdio.h>
int main() {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
赋值是个表达式, 可以作为更大的表达式的一部分:
#include <stdio.h>
int main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
}
!=
的优先级比 =
高.
1.5.2 字符计数
#include <stdio.h>
int main() {
long nc;
nc = 0;
while (getchar() != 'c') {
++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);
}
1.5.4 单词计数
#include <stdio.h>
#define IN 1
#define OUT 0
int main() {
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != '0') {
++nc;
if (c == '\n') {
++nl;
}
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
} else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
1.6 数组
#include <stdio.h>
int main() {
int c, i, nWhite, nOther;
int nDigit[10];
nWhite = nOther = 0;
for (i = 0; i < 10; ++i) {
nDigit[i] = 0;
}
while ((c = getchar()) != '0') {
if (c >= '0' && c <= '9') {
++nDigit[c-'0'];
} else if (c == ' ' || c == '\n' || c == '\t') {
++nWhite;
} else {
++nOther;
}
}
printf("digits = ");
for (i = 0; i < 10; ++i) {
printf(" %d", nDigit[i]);
}
printf(", white space = %d, other = %d\n",
nWhite, nOther);
}
1.7 函数
#include <stdio.h>
// 函数原型
int power(int, int);
int main() {
int i;
for (i = 0; i < 10; ++i) {
printf("%d %d %d\n", i, power(2, i), power(-3, i));
}
return 0;
}
int power(int base, int n) {
int i, p;
p = 1;
for (i = 1; i <= n; ++i) {
p = p * base;
}
return p;
}
1.8 参数 - 传值调用
好处:
- 减少函数内的变量声明, 直接使用形式参数
传递数组传的是数组第一个元素的地址, 所以函数对其修改可在外部可见.
1.9 字符数组
字符串常量自动使用 \0
结束.
"hello\n"
#include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxLine);
void copy (char to[], char from[]);
int main() {
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
printf("len: %d\n", len);
max = 0;
while ((len = getLine(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
}
if (max > 0) {
printf("%s", longest);
}
return 0;
}
int getLine(char s[], int lim) {
int c, i;
for (i=0; i<lim-1 && (c=getchar())!='0' && c!='\n'; ++i) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[]) {
int i;
i = 0;
while ((to[i] = from[i]) != '\0') {
++i;
}
}
1.10 外部变量与作用域
函数中的局部变量被称为自动变量.
extern:
- 如果外部变量在使用它的函数之前, 那么可以省略 extern 关键字
#include <stdio.h>
#define MAXLINE 1000
int max;
char line[MAXLINE];
char longest[MAXLINE];
int getLine(void);
void copy(void);
int main() {
int len;
extern int max;
extern char longest[];
max = 0;
while ((len = getLine()) > 0) {
if (len > max) {
max = len;
copy();
}
}
if (max > 0) {
printf("%s", longest);
}
return 0;
}
int getLine() {
int c, i;
extern char line[];
for (i=0; i<MAXLINE-1 && (c=getchar())!='0' && c!='\n'; ++i) {
line[i] = c;
}
if (c == '\n') {
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
void copy() {
int i;
extern char line[], longest[];
i = 0;
while ((longest[i] = line[i]) != '\0') {
++i;
}
}