第二章 C语言概述
一、典型C程序
(一)C程序构造
-
#include <stdio.h> 预处理器指令
-
int main(void) //main总是第一个被调用的函数,也是组成函数的语句
void表明main()函数不带任何参数
{}表示函数体的开始和结束
/* 注释,帮助我们更加容易理解与回顾 可以写作一行,也可以写作多行 。还可以使用//单行注释
*/ -
function a(); //函数时C程序的构造块 /*每行语句则必须以;结束
- int num; //声明变量类型以及名称----变量必须声明后,才能使用。建议声明置于函数体顶部。
-
function b();
-
return ;
-
}
(二)语句
语句由关键字,标识符,运算符和数据组成;语句分为以下几类:
- 1、标号语句
- 2、复合语句
- 3、表达式语句
- 4、选择语句
- 5、迭代语句
- 6、跳转语句
(三)常用概念
-
1、注释
-
2、声明
- int num; - num = 1; - 声明变量,常量时,编译器在系统内存中为变量预留了空间,然后在赋值语句执行时,把值储存在之前预留的位置。赋值操作从右至左,以分号结尾。
-
3、数据类型
- 如整数,字符,和浮点数
-
4、命名(标识符)
-
标识符定义方式:驼峰型AntMan和下划线性型hello_kitty
-
要求:大小写字母、数字和下划线(第一个字符必须是字母或下划线)
-
一般变量用小写,常量全大写,以下划线开始的标识符尽量避免
-
声明变量的理由
- 1、方便读者查找和理解程序的用途
- 2、促使编写程序之前做好计划
- 3、有助于发现隐藏在程序中的小错误
- 4、未声明的变量,C程序将无法通过编译
-
-
5、赋值
-
6、printf()
printf(“My favorte shoe is beautiful!”) //My favorte shoe is beautiful! 是函数的实际参数。区别于形式参数 是函数中用于储存值的变量。f是指明格式化打印函数。
- 7、转义序列
用于表达难以表示或无法输入的字符通过\实现。如\t代表制表符Tab,\n代表换行符,\b代表backspace(退格) - 占位符
%d指明位置
8、return
二、简单程序结构
一个程序必须由一个或多个函数组成,并且必须有main函数
#include <stdio.h>
int main(void) //main()函数
{
int num;
num = 1;
printf("I am a sample number\n");
printf("computer.\n");
printf("My favorite number is %d,because it is first.\n", num);
return 0;
}
三、 函数
函数由函数头和函数体组成
- 函数头包括函数名、传入该函数的信息类型和函数的返回类型。通过函数名后的()识别函数,括号内为函数参数(形式参数,可无)。
- 函数体被花括号括起来,由一系列语句声明组成
一个函数组成的程序:
#include <stdio.h>
int main(void)
{
int a = 10;
int b = 25;
printf("He's just a brother ,but you are %d",a*b);
return 0;
}
两个函数组成的程序:
//two_func,两个函数组成的程序
#include <stdio.h>
void butler(void); //ANSI/ISO**函数原型,也就是声明函数**
int main(void)
{
printf("I will summon the butler function.\n");
butler(); **//调用函数**
printf("Yes. Bring me some tea and writeable DVDs.\n");
system("pause");
return 0;
}
void butler(void) //函数定义,函数本身源代码 第一个void表明返回值为空,第二个void表明没有返回参数
{
printf("You rang, sir?\n");
}
四、调试程序
1.语法错误
//wrong code test代码查错
#include <stdio.h>
int main(void)
( //使用错误圆括号
int n,int n2, int n3; //该程序有多处错误,int n,n2,n3;
n = 5;
n2 = n*n;
n3 = n2 * n2;
printf("n = %d,n squared = %d, n cubed = %d\n", n, n2, n3 );
return 0;
)
2.语义错误
意思上的错误,如果C语法,结果不正确那就是犯了语义错误。这类错误,编译器无法检测。
如:
/*stillbad.c--修复了语法错误的程序*/
#include <stdio.h>
int main(void)
{
int n, n2, n3;
/*该处有一个语义错误*/
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d", n , n2, n3); //所求为n的平方、立方;但得到的却是n的平方,以及n2的平方
system("pause");
return 0;
}
3.程序状态
1、通过逐步跟踪程序的执行步骤,记录每个变量,可以监视程序的状态。程序状态实在程序执行过程中,某给定点上所有变量值得集合。
2、或者通过在关键点插入额外的printf()语句,以见识指定变量值的变化。
3、使用调试器,debugger。逐步运行程序,并检查该程序变量的值。
五、提高程序可读性的技巧
1.选择有意义的函数名
2.写注释
3.用空格分割概念上的不同部分
4.每条语句占一行
六、关键字
关键字不能用作标识符
32个常用关键字
auto :声明自动变量
short :声明短整型变量或函数
int: 声明整型变量或函数
long :声明长整型变量或函数
float:声明浮点型变量或函数
double :声明双精度变量或函数
char :声明字符型变量或函数
struct:声明结构体变量或函数
union:声明共用数据类型
enum :声明枚举类型
typedef:用以给数据类型取别名
const :声明只读变量
unsigned:声明无符号类型变量或函数
signed:声明有符号类型变量或函数
extern:声明变量是在其他文件正声明
register:声明寄存器变量
static :声明静态变量
volatile:说明变量在程序执行中可被隐含地改变
void :声明函数无返回值或无参数,声明无类型指针
if:条件语句
else :条件语句否定分支(与 if 连用)
switch :用于开关语句 case:开关语句分支
for:一种循环语句
do :循环语句的循环体
while :循环语句的循环条件
goto:无条件跳转语句
continue:结束当前循环,开始下一轮循环
break:跳出当前循环
default:开关语句中的“其他”分支
sizeof:计算数据类型长度
return :子程序返回语句(可以带参数,也可不带参数)循环条件
七、小结
- 一个C语言程序分别由一个或多个C函数组成,每个函数必须包含一个main()函数。
- 大部分语句都以分号结尾。
- 声明为变量创建变量名和标识该变量中储存的数据类型。
- 赋值表达式语句把值赋给变量,更为精确地说,是把值赋给存储空间。
- 函数表达式语句用于调用指定的已命名函数。调用函数执行完毕后,程序会返回到函数调用后面的语句继续执行。
- printf()函数用于输出想要表达的内容和变量的值
八、复习题
- 1、C语言的基本模块
语句 - 2、什么是语法错误,写出一个英语例子和C语言例子
不符合语言规范,或者C11规范的
I adult ,you child (省略动词)
int a;
a = 3.5; - 3、什么是语义错误,写出一个英语例子和C语言例子
- 4、Indiana Sloth编写了下面的程序,并征求你的意见。请你帮助他评定
include stdio.h //括号错误<>
int main{void} //该程序打印一年有多少周 括号错误()
( //中括号{}最末尾却是
int s //分号
s := 56; //等号前面:错误
print(There are s weeks in a year.); //printf内语句必须“”括起来
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.printf(“Baa Baa black sheep”); //
-
6、在main、int、function、char、=中,那些事C语言关键字?
-
7、如何以下面的格式输出变量words和lines的值(这里,3020和350代表两个变量的值)?
There were 3020 words and 350 lines.
#include <stdio.h>
int main(void)
{
int word = 3020;
int line = 350;
printf("There were %d words and %d lines", word, line);
system("pause");
return 0;
}
- 8、考虑下面的程序:
#include <stdio.h>
int main(void)
{
int a, b;
a = 5;
b = 2;/*第7行*/ //此时a = 5 b = 2
b = a;/*第8行*/ //此时 a = 5, b = 5
a = b;/*第9行*/ //此时 a = 5, b = 5
printf("%d %d\n", b, a );
system("pause");
return 0;
}
请问,在执行完第7、第8第9行后,程序的状态分别是什么?
- 9、考虑下面的程序:
/*#include <stdio.h>
int main(void)
{
int x,y;
x = 10;
y = 5; /*第7行*/ //此时 x = 10, y = 5
y = x + y; 第8行 //此时 x = 10, y = 15
x = x * y; /*第9行*///此时 x = 150,y = 15
printf("%d %d\n", x, y);
system("pause");
return 0;
请问,在执行完第7、第8、第9行后,程序的状态分别是什么?
九、编程练习
- 1、编写一个程序1,调用一次printf()函数,把你的姓名打印在以后。再调用一次printf()函数,把你的姓名分别打印在两行。然后,在调用两次printf()函数,把你的姓名打印在一行。
#include <stdio.h>
int main(void)
{
printf("Lance Bordeaux\n");
printf("Lance\nBordeaux\n");
printf("Lance Bordeaux\n");
getch();
return 0;
}
- 2、编写一个程序,打印你的姓名和地址。
#include <stdio.h>
int main(void)
{
printf("My name is Lance Bordeaux\n");
printf("My address is No.251 in Jiashi\n");
getch();
return 0;
}
- 3、编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。
#include <stdio.h>
int main(void)
{
int age, days;
age = 24;
days = 365;
printf("I am %d years old, and I lived %d days\n", age, age*days);
getch();
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(),用于打印前三条消息,调用一次,打印一跳;另一个名为deny(),打印最后一条消息。
#include <stdio.h>
void jolly(void);
void deny(void);
int main(void)
{
jolly();
jolly();
jolly();
deny();
getch();
}
void jolly(void)
{
printf("For he's a jolly good fellow!\n");
}
void deny(void)
{
printf("Which nobady can deny!\n");
}
- 5、编写一个程,生成一下输出:
Brazil, Russia,India, China
India, China
Brazil, Russia
除了main()以外,该程序还要调用两个自定义函数:一个名为br(),调用一次打印一次‘Brazil. Russia’;另一个名为ic(),调用一次打印一次‘India, China’。
其他内容在main()函数中完成。
#include <stdio.h>
void br(void);
void ic(void);
int main(void)
{
br(), ic();
ic();
br();
getch();
}
void br(void)
{
printf("Brazil,Russia\n");
}
void ic(void)
{
printf("India,China\n");
}
- 6、编写一个程序,创建一个整型变量toes,并将toes设置为10.程序中还要计算toes的两倍和toes的平方。该程序打印三个值,并分别描述,加以区分。
#include <stdio.h>
int main(void)
{
int toes, toes2, toes3;
toes = 10;
toes2 = 0;
toes3 = 0;
toes2 = toes * toes;
toes3 = toes2 * toes;
printf("toes is %d, toes squard is %d,toes cubed is %d\n",toes, toes2, toes3);
getch();
return 0;
}
- 7、许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
Smile!Smile!Smile!
Smile!Smile!
Smile!
该程序要定义一个函数,该函数被调用一次打印一次Smile!,根据程序的需要使用该函数。
#include <stdio.h>
int Smile(void);
int main(viod)
{
Smile()+Smile()+Smile();
Smile()+Smile();
Smile();
getch();
return 0;
}
int Smile(void)
{
printf("Smile!");
return 0;
}```
- 8、在C语言中,函数可以调用另一个函数,编写一个程序,调用一个名为one_three()的函数。该函数在一行打印one,在调用第二个函数two(),然后在另一行打印单词‘three,two()函数在一行显示单词two。main()函数在调用one_three()函数前要打印短语starting now:并在调用完毕后显示短语done。因此,该程序的输出应如下所示:
starting now:
one
two
three
done!
#include <stdio.h>
int two();
int one_three();
int main(void)
{
printf(“Starting now:\n”);
one_three();
getch();
return 0;
}
int two(void)
{
printf(“two\n”);
return 0;
}
//int two(void);
int one_three(void)
{
printf(“one\n”);
two();
printf(“three\n”);
return 0;
}