现在的位置: 首页 > 综合 > 正文

c 语言入门最简单示例——类似HellWord

2013年12月07日 ⁄ 综合 ⁄ 共 1082字 ⁄ 字号 评论关闭

ubuntu环境下

1、c61501.c文件内容

#include <stdio.h>
int main()
{
 int i,sum=0;
 for(i=1;i<=100;i++){
  	sum+=i;
 }
 printf("1+2+3+……+100=%d\n",sum);
 return 0;
} 

2、编译

gcc c61501.c -o sum

3、效果

预备条件:

pateo@pateo-B86N53X:~$ gcc --version
gcc (Ubuntu 4.3.5-3ubuntu1) 4.3.5
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

pateo@pateo-B86N53X:~$ g++ --version
g++ (Ubuntu 4.3.5-3ubuntu1) 4.3.5
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

简单的说明,针对c61501.c文件,的main函数有返回值,其在java里面的main函数是固定的写法,没有返回值,但是c可以有可以没有,这个是为什么呢,我请教了下同事,主要原因是为了便于查这个函数执行的状态,可以通过echo $?来查看执行状态

pateo@pateo-B86N53X:~/work/study$ gcc c61501.c -o sum
pateo@pateo-B86N53X:~/work/study$ ./sum
1+2+3+……+100=5050
pateo@pateo-B86N53X:~/work/study$ echo $?
0

头文件目录

pateo@pateo-B86N53X:~$ ls /usr/include/st
stab.h       stdint.h     stdio_ext.h  stdio.h      stdlib.h     string.h     strings.h    stropts.h    
pateo@pateo-B86N53X:~$ cd  /usr/include/
pateo@pateo-B86N53X:/usr/include$ cat stdio.h

抱歉!评论已关闭.