第二章 C语言概述

2.1 简单的C程序示例

我们先来看一个简单的C程序示例代码,该程序演示了C语言的一些基本特性。

#include <stdio.h>
int main(void){ /* 一个简单的C程序 */
    int num; /* 定义一个名为num的变量 */
    num = 1; /* 为num赋一个值 */
    printf("I am a simple "); /* 使用printf()函数 */
    printf("computer.\n");
    printf("My favorite number is %d because it is first.\n", num);
    return 0;
}

如果一切运行正常,该程序会在命令行上打印一些东西。

2.2 示例解释

一个典型的C程序会包含一下几个部分:
- #include —— 预处理器指令
- int main(void) —— 主函数(语句)
- function a() —— 语句
- function b() —— 语句
- 函数是C程序的构造块
其中语句包含:标号语句、复合语句、表达式语句、选择语句、迭代语句、跳转语句

2.2.1 第1遍:快速概要

            #include <stdio.h>

该行告诉编译器把 stido.h 中的内容包含在当前程序中。stdio.h 是C编译器软件包的标准部分,它提供键盘输入和屏幕输出支持。

            int main(void)

C程序中包含一个或多个函数,他们是C程序的基本模块。圆括号表明 main() 是一个函数名。int 表明函数的返回一个整数,void 表明函数不带任何参数。intvoidANSI C 标准定义函数的一部分。

            /* 一个简单的C程序 */

注释在 /* 和 */ 两个符号之间,这些注释能提高程序的可读性。注意,注释只是为了帮助读者理解程序,编译器会忽略他们。

            {}

左花括号表示函数定义开始;右花括号表示函数定义结束。

            int num; 

该声明,将使用一个名为 num 的变量,而且 num 是 int (整数)类型。

            num = 1;

把值 1 赋给名为 num 的变量。

            printf("I am a simple");

该语句使用 printf() 函数,在屏幕上显示 I am a simple ,光标停在同一行。在程序中这种使用函数被称为函数的调用。

            print("computer.\n");

该语句同样也是调用 printf() 函数,不同的是 \n 告诉计算机把光标移至下一行。

            printf("My favorite number is %d because it is first.\n", num);

该语句不同的是 %d 告诉计算机以什么形式输出 num的值以及打印在何处。

            return 0;

C函数可以给调用方法返回一个数。

2.3 多个函数

#include <stdio.h>
void butler(void); /* ANIS/ISO C函数原型 */
int main(int argc, const char * argv[]) {
    printf("I will summon the bulter function.\n");
    butler();
    printf("Yes. Bring me some tea and writeable DVDs.\n");
    return 0;
}

void butler(void){ /* 函数定义开始 */
    printf("You rang, sir?\n");
}

butler() 函数在程序中出现了3次。第1次是 函数原型(prototype),告知编译器在程序中要使用该函数;第2次以 函数调用(function call)的形式出现在 main() 中;第3次出现在 *函数定义(function definition)中,函数定义即是函数本身的源代码。

这里要注意,何时执行 butler() 函数取决于它在 main() 中被调用的位置,而不是 butler() 的定义在文件中的位置。

2.4 关键概念

编程是一件富有挑战性的事情。程序员要具备抽象和逻辑思维,并谨慎地处理细节问题(编译器会强迫你注意细节问题)。

2.5 本章小节

C程序由一个或多个C函数组成。每个C程序必须包含一个 main() 函数,这是C程序要调用的第1个函数。简单的函数由函数头和后面的一对花括号组成,花括号中是由声明、语句组成的函数体。

在C语言中,大部分语句都以分号结尾。声明为变量创建变量名和标识该变量中储存的数据类型。变量名是一种标识符。赋值表达式语句把赋值给变量,或者更一般地说,把值赋给存储空间。函数表达式语句用于调用指定的已命名函数。调用函数执行完毕后,程序会返回到函数调用后面的语句继续执行。

printf() 函数用于输出想要表达的内容和变量的值。

一门语言的 语法 是一套规则,用于管理语言中各有效语句组合在一起的方式。语句的 语义 是语句要表达的意思。编译器可以检测出语法错误,但是程序里的语义错误只有在编译完之后才能从程序的行为中表现出来。检查程序是否有语义错误要跟踪程序的状态,即程序每执行一步后所有变量的值。

2.6 复习题

1.C语言的基本模块是什么?
答:它们都叫作函数。

2.什么是语法错误?写出一个英语例子和C语言例子。
答:语法错误:违反了组成语句或程序的规则。
英文例子:Me speak English good.
C语言例子:printf”Where are the parentheses?”;

3.什么是语义错误?写出一个英语例子和C语言例子。
答:语义错误:是指含义错误。
英文例子:This sentence is excellent Czech.
(这句话的翻译是:这句话是出色的捷克人)
C语言例子:thrice_n = 3 + n;
(这句话的本意是求 n 的 3 倍)

4.Indiana Sloth 编写了下面的程序,并征求你的意见。请帮他评定。

include studio.h
int main{void} /* 该程序打印一年有多少周 /*
(
    int s

    s := 56;
    print(There are s weeks in a year.);
    return 0;

答:

#include <stdio.h> 有错误
int main(void) /* 该程序打印一年有多少周 */ 有错误
{ 有错误
    int s; 有错误

    s = 56; 有错误
    print("There are %d weeks in a year.", s); 有错误
    return 0;
} 有错误

5.假设下面的4个例子都是完整程序中的一部分,他们都输出什么结果?

a. printf("Baa Baa Black Sheep.");
   printf("Have you any wool?\n");
b. printf("Begone!\nO creature of lard!\n");
c. printf("What?\nNo/nfish?\n");
d. int num;
   num = 2;
   printf("%d + %d = %d",num, num, num + num);

答:a.、Baa Baa Black Sheep.Have you any wool?
b、Begone!
O Creature of lard!
c.、what?
No/nfish?
d、2 + 2 = 4

6.在 main、int、function、char、= 中,哪些是C语言的关键字?
答:int、char
7.如何以下面的格式输出变量 words 和 lines 的值(这里,3020 和 350代表两个变量的值)?
There were 3020 words and 350 lines.
答:int words = 3020;
int lines = 350;
printf(“There were %d words and %d lines.”, words, lines);

8.考虑下面的程序:

# include <stdio.h>
int main(void){
    int a, b;

    a = 5;
    b = 2; /* 第7行 */
    b = a; /* 第8行 */
    a = b; /* 第9行 */
    printf("$%d %d\n", b, a);
}

请问,在执行完第7、第8、第9行后,程序的状态分别是什么?
答:第7行完:a的值为5,b的值为2;
第8行完:a的值为5,b的值为5;
第9行完:a的值为5,b的值为5;

9.考虑下面的程序:

# include <stdio.h>
int main(void){
    int x, y;

    x =10;
    y = 5; /* 第7行 */
    y = x + y; /* 第8行 */
    x = x * y; /* 第9行 */
    printf("%d %d\n", x, y);
    return 0;
}

请问,在执行完第7、第8、第9行后,程序的状态分别是什么?
答:第7行完:x的值为10,y的值为5;
第8行完:x的值为10,y的值为15;
第9行完:x的值为150,y的值为15;

2.7 编程练习

1、 编写一个程序,调用一次 printf() 函数,把你的姓名打印在一行。再调用一次 printf() 函数,把你的姓名分别打印在两行。然后,再调用两次 printf() 函数,把你的姓名打印在一行。输出应如下所示:
Gustav Mahler <-第1次打印的内容
Gustav <-第2次打印的内容
Mahler <-仍是第2次打印的内容
Gustav Mahler <-第3次和第4次打印的内容
答:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    printf("Gustav Mahler\n");
    printf("Gustav\nMahler\n");
    printf("Gustav ");
    printf("Mahler\n");
    return 0;
}

2、 编写一个程序,打印你的姓名和地址。
答:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    printf("Name: KaraShok Zhang\n");
    printf("Address: BeiJing\n");
    return 0;
}

3、 编写一个程序把你的年龄转换成天数,并显示这两个值。这里不考虑闰年的问题。
答:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int age = 25;
    int days = 365;
    printf("Age: %d, Days: %d\n", age, age * days);
    return 0;
}

4、 编写一个程序,生成以下输出:
For he’s a jolly good fellow!
For he’s a jolly good fellow!
For he’s a jolly good fellow!
Which nobody can deny!
除了 main() 函数以外,该程序还要调用两个自定义函数:一个名为 jolly(),用于打印前3条消息,调用一次打印一条;另一个函数名为 deny(),打印最后一条消息。
答:

#include <stdio.h>
/* ANIS/ISO C函数原型 */
void jolly(void);
void deny(void);

int main(int argc, const char * argv[]) {
    jolly();
    jolly();
    jolly();
    deny();
    return 0;
}

void jolly(void){
    printf("For he's a jolly good fellow!\n");
}

void deny(void){
    printf("Which nobody can deny!\n");
}

5、编写一个程序,生成以下输出:
Brazil, Russia, India, China
India, China
Brazil, Russia
除了 main() 以外,该程序还有调用两个自定义函数:一个名为 br(),调用一次打印一次“Brazil,Russia“;另一个名为 ic() ,调用一次打印一次“India,China“。其他内容在 main() 函数中完成。
答:

#include <stdio.h>
/* ANIS/ISO C函数原型 */
void br(void);
void ic(void);

int main(int argc, const char * argv[]) {
    printf("Brazil, Russia, India, China\n");
    ic();
    br();
    return 0;
}

void br(void){
    printf("Brazil, Russia\n");
}

void ic(void){
    printf("India, China\n");
}

6、编写一个程序,创建一个整型变量 toes,并将 toes 设置为 10。程序中还要计算 toes 的两倍和 toes 的平方。该程序应打印3个值,并分别描述以示区分。
答:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int toes = 10;
    int square = toes * toes;
    int cube = square * toes;
    printf("tose: %d, square: %d, cube: %d\n", toes, square, cube);
    return 0;
}

7、许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
Smile! Smile! Simle!
Smile! Smile!
Smile!
答:

#include <stdio.h>
/* ANIS/ISO C函数原型 */
void smile(void);
void enter(void);

int main(int argc, const char * argv[]) {
    smile();
    smile();
    smile();
    enter();
    smile();
    smile();
    enter();
    smile();
    enter();
    return 0;
}

void smile(void){
    printf("Smile!");
}

void enter(void){
    printf("\n");
}

8、在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为另一个函数。编写一个程序,调用一个名为 one_three() 的函数。该函数在一行打印单词“one“,再调用第2个函数 two(),然后在另一行打印单词“three“。 two() 函数在一行显示单词“two“。main() 函数在调用 one_three() 函数前要打印短语“starting now:“,并在调用完毕后显示短语“done!“。因此,该程序的输出应如下所示:
starting now:
one
two
three
done!
答:

#include <stdio.h>
/* ANIS/ISO C函数原型 */
void onr_three(void);
void two(void);

int main(int argc, const char * argv[]) {
    printf("starting now:\n");
    onr_three();
    printf("done!\n");
    return 0;
}

void onr_three(void){
    printf("one\n");
    two();
    printf("three\n");
}

void two(void){
    printf("two\n");
}

猜你喜欢

转载自blog.csdn.net/karashok/article/details/80387106