#include "stdio.h"int main(){ /*一个简单的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 frist. \n",num);return 0;}
示例解释
典型的C程序||_#include --- 预处理器指令||_int main(void) ---main()总是第一个被调用的程序|__语句 ----组成函数的语句||_function a() ---函数是C程序的构造块|__语句|_function b()|__语句(C语言中的6种语句)|_标号语句 ---| ||_复合语句 ---| ||_表达式语句---| |---->C语言(关键字,标识符,运算符,数据)|_选择语句-----| ||_迭代语句----| ||_跳转语句-----
进一步使用C
#include "stdio.h"int main(){ /*一个简单的C程序*/int feet, fathoms;/*多条声明*/fathoms = 2;feet = fathoms * 6;printf("There are %d feet in %d fathoms!\n",feet,fathoms);/*打印了多个值*/printf("Yes, I said %d feet!\n",fathoms * 6);return 0;}
多个函数
/*two_func.c -- 一个文件中包含俩个函数*/#include "stdio.h"//声明函数void butler(void); /*ANSI/ISO C函数原型*/int main(void){printf("I will summon the butler function. \n");butler();printf("Yes, Bring me some tea and writeable DVDs. \n");return 0;}void butler(void){/*函数定义开始*/printf("You rang, sir? \n");}
关键字和保留标识符
ISO 关键字
| auto | extern | short | while |
|---|---|---|---|
| break | float | signed | _Aligmas |
| 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_local |
小结
C程序由一个或多个C函数组成。每个C程序必须包含一个main()函数,这个C程序要调用的第一个函数。简单的函数由函数头和后面的一对花括号组成,花括号中是由声明、语句组成的函数体
编程练习
天羽 陈天羽陈天羽 陈 <--俩次printf
编写答案
#include "stdio.h"int main(void){char fristName[] = "陈", lastName[]="天羽";printf("%s %s\n",lastName,fristName);printf("%s\n",lastName);printf("%s\n",fristName);printf("%s",lastName);printf("%s\n",fristName);return 0;}
