linux上的第一个c语言程序

1、编辑源文件
  输入命令如下:
  root@ubuntu:/home/amy# vi hello.c

  文件内容如下:
  #include<stdio.h>
  int main()
  {
    printf("hello world!\r\n");
  }

2、编译hello.c文件
  root@ubuntu:/home/amy# gcc hello.c -o hello
2.1、报错如下
  The program 'gcc' can be found in the following packages:
  * gcc
  * pentium-builder
  Try: apt-get install <selected package>
  -bash: gcc: command not found

  解决方法:

    百度一下,发现ubuntu linux下默认没有安装g++的,需要自己手动安装。在shell下执行apt-get install build-essential。

  root@ubuntu:/home/amy# apt-get install build-essential
  安装完毕后执行,没有错误信息。

3、执行hello
  root@ubuntu:/home/amy# ./hello
结果如下:

  

 

带输入参数的c程序

  源代码为:

#include<stdio.h>
int main(int argc, char * argv[])
{
    printf("hello %s!\r\n",argv[1]);
}

   编译和执行结果如下:

  

  

  

 

posted on 2014-07-25 17:18  BestNow  阅读(283)  评论(0编辑  收藏  举报

导航