Linux终端输入vim hello.c进入hello.c文本,此时处于一般命令模式。输入i进入插入模式。输入
#include
#include
void main()
{
printf("hello world!\n");
exit(0);
}
输入esc进入一般命令模式,再输入:wq保存并退出。
hello.c - 预处理 - 编译 - 汇编 - 链接 - 可执行文件。
2.1 预处理:终端输入gcc -E hello.c > hello.i 预处理保存在hello.i
2.2 编译: 终端输入gcc -S hello.i 编译生成hello.s文件
2.3汇编: 终端输入gcc -c hello.s 汇编生成hello.o目标文件
2.4链接:终端输入 gcc hello.o -o helloplay 生成helloplay可执行文件
2.5执行:终端输入 ./helloplay 执行
终端输入man gcc可以查看gcc手册
快捷执行法: 1. gcc hello.c自动生成a.out可执行文件。
或者 gcc hello.c -o helloplay生成helloplay可执行文件。
或者 make hello 直接生成hello可执行文件
3. ./a.out 或者./ helloplay
一般模式esc,插入模式i,命令行模式:,视图模式v。
3.1调整缩进格式:在一般模式,按v进入视图模式。鼠标选中所要缩进的代码,按 = 号实现自动缩进。
3.2设置缩进格数:终端 cp /etc/vimrc ~/.vimrc 把主vim配置文件拷贝到自己用户下,在修改自己的配置时不会影响其他人的。vim ~/.vimrc 访问配置文件,添加 set tabstop=8把自己的缩进修改为8个空格。
4.1 vim 文件名,编辑文件
4.2 man 查看一些关键字的详细信息。比如 man gcc , man malloc
4.3 rm 删除文件
4.4 gcc -E hello.c 预处理等
4.5 gcc hello.c -o hello直接生成可执行文件
4.6 make hello直接生成可执行文件
4.7 ./ 执行文件
4.8 gcc 文件名 -Wall 查看报错
4.9 echo $? 查看上句话的返回值
1.终端gcc a.c -Wall查看报错。
2.例子,下面代码可以删除头文件,查看报错内容。比如调用strerorr()方法,因没有头文件string.h报错。使用exit(),没有头文件stdlib.h报错。
#include
#include
#include
#include
int main()
{
FILE *fp;
fp = fopen("tmp","r");
if(fp == NULL)
{
fprintf(stderr,"fopen():%s\n",strerror(errno));
exit(1);
}
puts("ok!");
exit(0);
}
Return 0;结束当前函数,给父进程看0
exit(0);结束当前进程
查看上句返回值:终端 echo $?
例子printf()返回值为输出语句的字节数
注释 #if 0 #endif
数据类型
类型转换:隐式,默认精度高。Char+int =int
显式
不同形式的0值:0,‘0’,“0”,‘\0’
常量:整型,实型,字符(‘a’'\015'转义字符),字符串(“axv”),标识(#define)
#define PI 3.1415926
#define ADD 2+3
#define ABB (2+3)
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int i = 5 , j = 3;
printf("%f\n",PI*ADD);
printf("%d\n",ADD * ADD);
printf("%d\n",ABB*ABB);
printf("i=%d,""j=%d\n",i,j);
printf("%d\n",MAX(i++,j++));
printf("i=%d,j=%d\n",i,j);
exit(0);
}
输出:
[tom@CentOS7 ~]$ make define
cc define.c -o define
[tom@CentOS7 ~]$ ./define
9.283185
11
25
i=5,j=3
6
i=7,j=4