C 语言教程 在线

1444C 基本语法

C关键字:

按年份起始:

  • auto      break     case       char      const         continue  default     do 
  • double  else       enum      extern   float           for             goto         if 
  • int long register return short signed sizeof static 
  • struct   switch    typedef  union    unsigned  void            volatile   while

1999年12月16日,ISO推出了C99标准,该标准新增了5个C语言关键字:

  • inline    restrict    _Bool   _Complex    _Imaginary

2011年12月8日,ISO发布C语言的新标准C11,该标准新增了7个C语言关键字:

  • _Alignas  _Alignof  _Atomic  _Static_assert  _Noreturn  _Thread_local  _Generic  

1443C 程序结构

"引用(引用自楼上@军): 因编译器的原因,生成的 *.exe 文件打开时会一闪而过,从而观察不到其运行的结果,这是因为 main() 函数结束时,DOS 窗口会自动关闭",如果不想使用 system("pause")函数,可以直接使用cmd运行编译的可执行文件:
  • 1、在 *.exe 文件目录下建一个 *.bat 文件(建一个文本文档,强制改后缀为 bat)。
  • 2、用记事本(或其他编辑器)打开,写命令: cmd.exe cd [编译器生成的 *.exe 所在目录]。
  • 3、运行这个 *.bat 就会自动定位到当前 exe 的目录了,接下来只要敲你自己生成的程序的名称,就可以看到结果而不闪退。
  • 4、补充: 你也可以直接开 cmd 直接 cd 到当前目录,只要用 cmd 运行 *.exe 都可以看见结果,除非你自己编译的程序本身就无法运行。

1442C 程序结构

因编译器的原因,生成的 .exe 文件打开时会一闪而过,从而观察不到其运行的结果,这是因为 main() 函数结束时,DOS 窗口会自动关闭。为了避免这个问题可在 return 0; 前加入 system("pause"); 语句。

#include <stdio.h>
int main()
{
   /* 我的第一个 C 程序 */
   printf("Hello, World! \n");
   system("pause");      //暂停函数,请按任意键继续...
   return 0;
}

使用 gcc hello.c -o hello 命令时,可不添加目标路径,则 gcc 即在当前工作目录自动生成 hello.exe 文件。

1441C 程序结构

gcc 命令如果不指定目标文件名时默认生成的可执行文件名为 a.out(linux)a.exe(windows)

可用 gcc [源文件名] -o [目标文件名] 来指定目标文件路径及文件名。

例如,windows 系统上,gcc hello.c -o target/hello 会在 target 目录下生成 hello.exe 文件(Linux 系统生成 hello 可执行文件),target 目录必须已存在,[源文件名] 和 -o [目标文件名] 的顺序可互换, gcc -o target/hello hello.c 依然有效。

1440C 环境设置

gcc 进行 c 语言编译分为四个步骤:

1.预处理,生成预编译文件(.i 文件):

gcc –E hello.c –o hello.i

2.编译,生成汇编代码(.s 文件):

gcc –S hello.i –o hello.s

3.汇编,生成目标文件(.o 文件):

gcc –c hello.s –o hello.o

4.链接,生成可执行文件:

gcc hello.o –o hello

有时候,进行调试,可能会用到某个步骤哦