编写一个C程序

1.用文本文件编写代码

用 touch 命令:“touch 文件名” 可以创建一个文件(比如 touch hello.c),如下图:
在这里插入图片描述
在命令行输入 touch hello.c ,就在文件夹中创建了一个hello.c文件,打开文件编写我们著名的 “hello world ” 并保存

#include<stdio.h>
#include<stdlib.h>
int main()
{
	printf("Hello,World\n");
	return 0;
}

2.编译文件

现在我们已经写好了一个C代码。在命令行使用命令:gcc -o 指定文件名 文件(gcc -o hello hello.c)
在这里插入图片描述
这样我们就编译好了这个代码,重新打开文件夹(一般是home)会发现多了一个hello文件
在这里插入图片描述
然后我们在命令行输入: ./文件名(./hello) 就能得到输出(“.” 代表当前目录),如下所示
在这里插入图片描述

值得注意的是,

上面 的 -o name 选项是告诉编译器可执行程序的名字,就是后面生成的hello文件,如果你忘记提这个,那么编译器会把程序放在一个名为:a.out 的文件里(a.out的含义是seeember output,即汇编输出)。如果你确信编译了一个程序但又找不到它,别忘了看看有没有a.out文件。

例子如下:
在这里插入图片描述

GitHub 加速计划 / li / linux-dash
6
1
下载
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐