Linux学习笔记12--我的第一个C程序

本文介绍了C语言编程的基础,包括创建、编译和运行简单的C程序。通过实例演示了常见错误排查过程,并展示了如何改进输出格式。

今天学习点c语言编程,学习c语言编程时要了解如何编译调式及运行c语言程序。

先使用vi新建一个mytestc.c文件。
并且输入相关的c程序代码如下操作:
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
in main()
{
        printf("Hello,this is my first program.");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" [New] 5L, 77C written
  
--开始编译。
--gcc主要时用来编译c/c++程序的。
--这里只用到gcc的简单参数-o 和 -g。
-- -o 表示设定输出文件名。
-- -g 表示加入调式信息,为gdb准备                         
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c 
mytestc.c:2: error: syntax error before "main"
--编译错误,提示"main"前面语法错误,这里再使用vi来修改。发现main()前的数据类型int写成了in 比较晕哦,太马虎了。
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
int main()
{
        printf("Hello,this is my first program.");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" 5L, 78C written                                  
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c
[OK_008@CentOS4 ~]$ ls
--没有任何提示,说明测试通过,使用ls,我们发现目下下多了个编译后的mytestc文件。
debugtest    Desktop  mytestc.c   mytxt.txt   TestFile
debugtest.c  mytestc  mytxt1.txt  OK008Filed  untar
[OK_008@CentOS4 ~]$ ./mytestc 
--该命令是执行刚才生成的mytestc文件。
Hello,this is my first program.[OK_008@CentOS4 ~]$ --这里是执行结果,ok成功了
--为了好看,我修改一下程序加个回车。
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
int main()
{
        printf("Hello,this is my first program.\n");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" 5L, 80C written
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c
--重新编译
[OK_008@CentOS4 ~]$ ./mytestc
Hello,this is my first program.
  --成功。
[OK_008@CentOS4 ~]$

原来在linux下编译c也不是很难。呵呵,鼓励一下自己,忽悠呀。
其实gcc命令中还有很多的参数没有使用。
这里也没有使用到gdb调试工具来调式代码,还有可以make和makefile来完成自动维护编译工作,功能不错。
到用到的时候再具体了解。

参考资料:《Linux 系统开发员》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值