Linux(ubuntu) C语言

一、编辑器

  1. emacs 、vim 两种编辑器,编辑器之神和神的编辑器

二、软件安装

  1. 安装vim前先更新 (sudo 管理员权限)

    sudo apt-get update  
    
  2. 安装vim

    sudo apt-get install vim
    
  3. 查看GCC编译器版本

    cc -v 
    

三、常用指令

  1. Linux常用命令

    cd ~  进入home目录
    ls -l  显示文件列表
    ls / 查看根目录
    touch xxx  创建文件
    rm xxx   删除文件
    mkdir  创建目录
    cd xxx  进入目录
    cp main.c main2.c 拷贝main.c,命名为main2.c
    
  2. vim编辑器命令

    :w  保存 
    :wq 保存并退出
    :i  光标前插入字符  
    a  光标后插入字符 
    esc  退出 
    shift + i 行首插入字符  
    shift + a  行尾插入字符 
    o 当前行下一行插入字符 
    shift+o 当前行上一行插入字符 
    x 删除一个字符 
    dd 删除一行
    命令模式下 sp 打开多个文件  ctrl+w+向下箭头  光标跳到下方一个文件
    

四、makefile文件编写

  1. make工具安装
make -v #查看版本
sudo apt-get install make  #安装
  1. makefile文件
#makefile文件编写 gcc命令前按下tab键(linux的tab键表示8个空格)
hello.out:max.o min.o hello.c -o hello.out
	gcc max.o min.o hello.c
max.o:max.c
	gcc -c max.c
min.o:min.c
	gcc -c min.c


五、C语言main函数

1. main函数的完整形式

#include <stdio.h>
int main(int argv, char* argc[])
{
    printf("hello world \n");
    return 0;
}

gcc main.c -o main.out && ./main.out #命令连接执行
echo $?  #输出是0,表示程序正常执行,跟main函数return的数值一致。前一个程序不是返回0,后一个程序不会被执行

2. main函数的参数

#include <stdio.h>
int main(int argv, char* argc[])
{
    //argv表示命令行中的参数个数
    printf("argv is %d \n", argv);
    int i;
    for(i=0;i<argv;i++){
        printf("argc[%d] is %s \n", i, argc[i]);
    }
    return 0;
}

六、标准输入、输出以及错误流

#include <stdio.h>
/*
stdin
stdout
stderr
*/
int main()
{
    //printf("please input the value a:\n");
    fprintf(stdout, "please input the value a:\n");
    int a;
    //scanf("%d", &a);
    fscanf(stdin, "%d",&a); 
    if(a<0){
        fprintf(stderr,"the value must > 0\n");
        return 1;
    }
    return 0;
}

七、标准输入、输出以及错误流的重定向

双箭头表示追加,单箭头表示覆盖

txt文本代替键盘或者显示器等输入输出设备

#输出重定向
./a.out 1>> a.txt 或者   ./a.out >> a.txt
 cat a.txt
 ls /ect >> etc.txt
 cat etc.txt
  ls /ect > etc.txt  单箭头表示可看最新的
 # 输入重定向
 vi input.txt
 ./a.out < input.txt
#include <stdio.h>
*/
int main()
{   
    printf("input the int value i:\n");
    int i, j;
    scanf("%d", &i);
    printf("input the int value j:\n");
    scanf("%d", &j);
    if(0 != j){
        printf("%d / %d = %d\n", i, j, i/j);
    }else{
        fprintf(stderr, "j != 0\n");
        return 1;
    }
    return 0;
}
./a.out 1>t.txt 2>f.txt < input.txt
t.txt  标准输出流
f.txt  错误输出流
input.txt 输入流

八、管道原理及应用

ls /etc/ | grep ab  查询etc目录下文件名包含ab的文件
`|` 管道符,ls /etc/ 命令执行的结果做为grep命令的参数

ps -e 查看进程
ps -e | grep ssh 查询进程里有没有ssh进程

九、实用C语言小程序

# avg.c
#include <stdio.h>
int main()
{
    int s, n;
    scanf("%d,%d", &s,&n);
    float v = s / n;
    printf("v=%f \n",v);
    return 0;
}
cc avg.c -o avg.out
./avg.out
#input.c
#include <stdio.h>
int main()
{
    int flag = 1;
    int i;
    int count = 0;
    int s = 0;
    while(flag){
        scanf("%d", &i);
        if(0 == i) break;
        count++;
        s += i;
    }
    printf("%d,%d \n",s,count);
    return 0;
}
./input.out | ./avg.out
posted @ 2020-10-26 20:28  兴子  阅读(104)  评论(0)    收藏  举报