网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

linux命令行编程c linux c编程

怎么用linux写c语言

Linux正在成为开发人员的编程天堂,成为开源和免费操作系统。 Turbo C编译器已经是一种编译程序的旧方法,所以让程序员转向Linux以获得新的编程环境。 在本文中,我们将解释如何编写,编译和运行一个简单的C程序。 这将成为您迁移到可以在Linux上编写和执行的更复杂和有用的C程序的基础。

创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为新城企业提供专业的网站设计制作、做网站,新城网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

我们在Ubuntu 18.04 LTS系统上运行了本文中提到的步骤和命令。

我们将使用Linux命令行工具Terminal,以编译一个简单的C程序。 要打开终端,您可以使用Ubuntu Dash或Ctrl + Alt + T快捷方式。

第1步:安装build-essential软件包

为了编译和执行C程序,您需要在系统上安装必要的软件包。 在Linux终端中以root用户身份输入以下命令:

sudo apt-get install build-essential

系统会要求您输入root用户密码; 安装过程将在此之后开始。 请确保您已连接到互联网。

第2步:编写一个简单的C程序

安装必要的软件包之后,让我们编写一个简单的C程序。

打开Ubuntu的图形文本编辑器,将以下示例程序写入或复制到其中:

#includestdio.h

int main()

{

printf("nA sample C program ");

return 0;

}

然后使用.c扩展名保存文件。 在这个例子中,我将我的C程序命名为linuxidc.c

或者,您可以通过gedit中的终端编写C程序,如下所示:

gedit linuxidc.c

这将创建一个.c文件,您可以在其中编写和保存程序。

第3步:使用gcc编译C程序

在终端中,输入以下命令以生成您编写的程序的可执行版本:

句法:

$ gcc [programName].c -o programName

示例:

$ gcc linuxidc.c -o linuxidc

到底怎么在Linux里编写c程序啊?

在linux下通常使用gedit或vim直接编写.c程序,然后通过gcc指令编译。以Ubuntu系统为例,详细过程如下:

1、进入桌面Temp文件夹

2、右键新建空白文件

3、将文件命名为hello.c

4、进入hello.c,开始编写代码(默认gedit为编辑器)

5、编写代码,保存退出

6、点击右列“终端”,或者直接Ctrl+Alt+T通过快捷组合键进入终端

7、进入hello.c所在目录,通过gcc进行编译、链接、生成可执行文件hello,命令为gcc -o hello hello.c。

8、执行(可执行)文件hello,命令为./hello。

参考资料:

GCC——百度百科

Linux常用命令——百度百科

linux c编程

/*

Name: list.c

Author: guozan _SCS_BUPT

Mail: guozan523@foxmail.com

Date: 2010/4/6

实验目的:练习vi,使用UNIX的系统调用和库函数,体会UNIX文件通配符的处理方式以及命令对选项的处理方式。

编程实现程序list.c,列表普通磁盘文件(不考虑目录和设备文件等),列出文件名和文件大小。

与ls命令类似,命令行参数可以有0到多个

0个参数:列出当前目录下所有文件

参数为普通文件:列出文件

参数为目录:列出目录下所有文件

实现自定义选项r,a,l,h,m以及--

r 递归方式列出子目录

a 列出文件名第一个字符为圆点的普通文件(默认情况下不列出文件名首字符为圆点的文件)

l 后跟一整数,限定文件大小的最小值(字节)

h 后跟一整数,限定文件大小的最大值(字节)

m 后跟一整数n,限定文件的最近修改时间必须在n天内

-- 显式地终止命令选项分析

*/

#include sys/stat.h

#include sys/types.h

#include dirent.h

#include unistd.h

#include stdio.h

#include string.h

/*

slective options about ls

rflag is about recursive

aflag is about ones with . infront

lflag is about the minimum size

hflag is about the maximum size

mflag is about the modified time

*/

int rflag, aflag, lflag, hflag, mflag;

long modified_time; //the last time file be modified, days ago

off_t lower_size; //file's minimum size

off_t upper_size; //file's maximum size

/*

set the flags, thus the ls option

*/

void getoptions(int argc, char *argv[])

{

char ch;

//clear, all unseted

rflag = 0; aflag = 0; lflag = 0; hflag = 0; mflag = 0;

//use getopt to get the options, want to know more, call man

//the last one or after -- was set in argv[optind]

while ((ch = getopt(argc, argv, "ral:h:m:")) != -1) {

switch (ch) {

case 'r': rflag = 1; break;

case 'a': aflag = 1; break;

case 'l': lflag = 1; lower_size = atol(optarg); break;

case 'h': hflag = 1; upper_size = atol(optarg); break;

case 'm': mflag = 1; modified_time = atol(optarg); break; //get days

case '?': printf("Unknown option: %c\n", (char)optopt); break;

default : printf("Step into default\n"); break;

}

}

}

/*

the function to list things in path

*/

int ls(char *path)

{

struct stat st; //for check this is a directory or file

char temp[100]; //if path is null, it is used to get current directory

// get the path

if (path == NULL || path[0] == '-') {

path = temp;

getcwd(path, 100);

}

/* open the inode of file */

if (lstat(path, st)) {

fprintf(stderr, "Error: %s not exist.\n", path);

return (-1);

}

/* judge whether the file is a file or a directory */

if (S_ISDIR(st.st_mode)) {

ls_dir(path);

}

else if (S_ISREG(st.st_mode)) {

print(path);

}

else {

printf("Not ordinary file, wouldn't be listed.\n");

}

return 0;

}

/*

list dirs, may recursively or not, depending on rflag

one thing is sure that it will list directories and files first,

then consider the things in the directories

*/

int ls_dir(char *path)

{

DIR *dp = NULL;

struct dirent *dirp = NULL;

if (path[0] != '.' || (path[0] == '.' aflag == 1)) {

printf("\n%s:\n****************************************\n", path);

/* open the directory */

if ((dp = opendir(path)) == NULL) {

fprintf(stderr, "Error: can't open directory %s!\n", path);

return (-1);

}

chdir(path);

/* list all the things in directory */

while ((dirp = readdir(dp)) != NULL) {

print(dirp-d_name);

}

/* recursively ls dirs, after ls things together,

it's time to list things in children directory */

if (rflag == 1) {

rewinddir(dp); //reset dp

while ((dirp = readdir(dp)) != NULL) {

if (strcmp(dirp-d_name, ".") == 0

|| strcmp(dirp-d_name, "..") == 0) { //no current and parent directory

continue;

}

ls_dir_r(dirp-d_name); //only list directories, judged inside the function

}

}

/* close the directory */

if (closedir(dp)) {

fprintf(stderr, "Error: can't close the directory %s!\n", path);

return -1;

}

chdir("..");

}

return 0;

}

/*

list directories recursively,

only directories, nomatter what path you put in

*/

int ls_dir_r(char *path)

{

struct stat st;

/* open the inode of file */

if (lstat(path, st)) {

fprintf(stderr, "Error: %s not exist.\n", path);

return (-1);

}

/* only ls directories */

if (S_ISDIR(st.st_mode)) {

ls_dir(path);

}

}

/*

print the filetype/size/name on the screen

*/

int print(char *path)

{

struct stat st;

time_t tp;

char *filename = NULL;

//get current time

time(tp);

if (lstat(path, st)) {

fprintf(stderr, "Error: %s can't be opened.\n", path);

return (-1);

}

/* get file name */

if ((filename = strrchr(path, '/')) != NULL) {

filename++;

}

else {

filename = path;

}

/* judge whether to list the file */

if ((S_ISDIR(st.st_mode)|| S_ISREG(st.st_mode)) //only directories and normal files

(lflag == 0 || (lflag == 1 (st.st_size = lower_size))) //the min size

(hflag == 0 || (hflag == 1 (st.st_size = upper_size))) //the max size

(mflag == 0 || (mflag == 1 ((tp - st.st_mtime) = modified_time * 24 * 60 * 60))) //modified time

(aflag == 1 || (aflag == 0 filename[0] != '.')) //file with a '.' infront

) {

printf("%s\t%10ld\t%s\n", (S_ISDIR(st.st_mode) ? "DIR": "FILE"), st.st_size, filename);

}

return 0;

}

/*

The main function

*/

int main(int argc, char *argv[])

{

getoptions(argc, argv);

ls(argv[optind]);

return 0;

}


标题名称:linux命令行编程c linux c编程
文章路径:http://bjjierui.cn/article/ddoojpe.html

其他资讯