文章目录
第二章 C语言概述
/*2.1*/
#include<stdio.h>
int main(void){ //一个简单的c程序
int num; //定义一个名为num的变量
num = 1; //为num赋一个值
printf("I am a simple "); //使用printf()函数
printf("computer.\n");
printf("My favourite number is %d because it is the first.\n",num);
return 0;
}
I am a simple computer.
My favourite number is 1 because it is the first.
若程序在窗口运行时一闪而过,则在return语句前加上getchar(),再接收一次任意键后结束程序。
-
程序细节
-
#include指令和头文件
#include是一条c语言预处理器指令。
所有的c编译器都提供stdio.h文件,通常.h文件被称为头文件(head)。
大多数情况下,头文件包含了编译器创建最终可执行程序要用到的信息 -
main()函数
int main(void)
c程序一定从main()函数开始.
int为main()函数的返回类型,int表示返回的为整数。main()
C90勉强接受这种格式,C99和C11标准不允许这么写。void main()
一些编译器允许这么写,但是所有标准都未认可这种写法。 -
注释
有/**/和//两种形式
-
花括号、函数体和块
一般而言,所有的c函数都使用花括号表示函数的开始和结束,不能省略。
-
声明
在C语言中所有的变量必须先声明再使用。
以前的C语言中声明必须置于块的顶部,C99和C11都遵循c++的惯例,可以将声明置于任何位置。
-
-
tips
C语言共有六种语句:标号语句、复合语句、表达式语句、选择语句、迭代语句、跳转语句。
变量的命名可以用小写字母、大写字母、数字和下划线命名,而且名称的第一个字符必须是字母或下划线,不能为数字。
关键字是C语言的词汇,不能作为标识符(如变量名),下列关键字中,粗体为C90标准新增的关键字,斜体表示C99标准新增的关键字,粗斜体表示的是C11新增的关键字。
C关键字 | |||
---|---|---|---|
auto | extern | short | while |
break | float | signed | _Alignas |
case | for | sizeof | _Alignof |
char | goto | static | _Atomic |
const | if | struct | _Bool |
continue | inline | switch | _complex |
default | int | typedef | _Generic |
do | long | union | _Imaginary |
double | register | unsigned | _Noreturn |
else | restrict | void | Static_assert |
enum | return | volatile | Thread_lacal |
-
复习题
Q:C语言的基本模块是什么?
A:函数。Q:什么是语法错误?
A:违反组成语句或程序的规则。Q:什么是语义错误?
A:含义错误。Q:打印一年有多少周。
/*打印一年有多少周(错)*/ #include studio.h /*该程序打印一年有多少周/* int main{void} ( int s; s := 56; printf("there are s weeks in a year.\n"); return 0;
A:改:
/*打印一年有多少周*/ #include<stdio.h> /*该程序打印一年有多少周*/ int main(void) { int s; s = 56; printf("there are %d weeks in a year.",s); return 0; }
Q:
#include<stdio.h> int main(void) { int a,b; a=5; b=2;/*第七行*/ b=a;/*第八行*/ a=b;/*第九行*/ printf("%d %d\n",b,a); return 0; }
在执行完7、8、9行后程序分别是什么状态?
A:第七行时:a=5,b=2;
第八行时:a=5,b=5;
第九行时:a=5,b=5;Q:
#include<stdio.h> int main(void) { int x,y; x=10; y=5; /*第七行*/ y = x + y; /*第八行*/ x = x*y; /*第九行*/ printf("%d %d\n",x,y); return 0; }
在执行完7、8、9行后程序分别是什么状态?
A:第七行时:x=10,y=5;
第八行时:x=10,y=15;
第九行时:x=150,y=15;
-
编程练习
Q:编写一个程序,调用一次printf()函数,把你的姓和名打印在一行,再调用一次printf()函数,把你的姓和名分别打印在两行,然后再调用两次printf()函数,把你的姓和名打印在一行。
输出如下示例:Gustav Mahler <-第一次打印 Gustav <-第二次打印 Mahler <-第二次打印 Gustav Mahler <-第三次打印
A:
/*打印姓和名*/ #include<stdio.h> int main(void){ /*第一次*/ printf("Gustav Mahler\n"); /*第二次*/ printf("Gustav\nMahler\n"); /*第三次*/ printf("Gustav "); printf("Mahler"); return 0; }
Q:编写一个程序,打印姓名和地址。
A:/*打印姓名和地址*/ #include<stdio.h> #define name "NAME" #define address "ADDRESS" int main(void){ /*输出姓名*/ printf("%s\n",name); /*输出地址*/ printf("%s\n",address); return 0; }
Q:编写一个程序,把你的年龄转换为天数,不考虑闰年。
A:/*打印年龄转化为天数*/ #include<stdio.h> #define age_days 365 int main(void){ int age,days; printf("please input you age:"); /*输入年龄*/ scanf("%d",&age); /*计算年龄对应天数*/ days = age*age_days; /*输出对应天数*/ printf("the days are %d",days); return 0; }
Q:编写一个程序,生成以下输出:
For he is a jolly good fellow! For he is a jolly good fellow! For he is a jolly good fellow! Which nobody can deny!
除了main()函数以外,还要自定义一个名为jolly(),打印前三行,每次调用打印一条,另一个函数为deny(),用于打印最后一条消息。
A:/*打印消息*/ #include<stdio.h> void jolly(); //函数引用声明 void deny(); /*主函数main()*/ int main(void){ jolly(); jolly(); jolly(); deny(); return 0; } /*jolly()函数*/ void jolly(){ printf("For he is a jolly good fellow!\n"); } /*deny()函数*/ void deny(){ printf("Which nobody can deny!\n"); }
Q:编写一个程序,生成以下输出:
Brazil,Russia,India,China India,China Brazil,Russia
除了main()函数以外,还要调用两个自定义函数,一个为br()调用一次打印一次"Brazil,Russia",另一个为ic(),调用一次打印一次"India,China"。
A:/*打印国家名称*/ #include<stdio.h> void br(); //函数声明 void ic(); /*主函数*/ int main(void){ br(); printf(","); ic(); printf("\n"); ic(); printf("\n"); br(); printf("\n"); return 0; } /*br()函数*/ void br(){ printf("Brazil,Russia"); } /*ic()函数*/ void ic(){ printf("India,China"); }
Q:编写一个程序,创建一个整型变量toes,并将toes设置为10,计算toes的两倍和toes的平方,该程序打印三个值。
A:/*计算toes*2和toes^2*/ #include<stdio.h> int main(void){ int toes; /*输出toes的值*/ printf("%d\n",toes); /*输出toes*2的值*/ printf("%d\n",toes*2); /*输出toes*toes的值*/ printf("%d\n",toes*toes); return 0; }
Q:编写一个程序,生成以下的输出:
Smile!Smile!Smile! Smile!Smile! Smile!
该程序要定义一个函数,该函数被调用一次输出一次smile!
A:/*输出smile*/ #include<stdio.h> void smile(){ printf("Smile!"); } int main(void){ /*打印第一行*/ smile();smile();smile(); printf("\n"); /*打印第二行*/ smile();smile(); printf("\n"); /*打印第三行*/ smile(); return 0; }
Q:编写一个程序,调用一个名为one_three()的函数,函数在第一行打印one,第二行调用two()函数,然后在第三行打印three,two()函数在第二行打印two,主函数在one_three()函数调用前要打印短语"starting now:",并在调用结束后输出短语"done!"。
该程序结果如下图所示:starting now: one two three done!
A:
/*函数嵌套输出*/ #include<stdio.h> void one_three(); void two(); int main(void){ printf("starting now:\n"); one_three(); printf("done!\n"); return 0; } void one_three(){ printf("one\n"); two(); printf("three\n"); } void two(){ printf("two\n"); }
2021.12.23