• Linux C语言编程基本原理与实践 笔记 gcc max.o hello.c


    一、简介

    人类和计算机交流的一种方式。

    C语言适合做Linux嵌入式。小工具。

    MAC电脑是Unix内核。

    二、Linux基本操作

    #vi a.c新建文件

    #rm a.c删除文件

    i 当前光标前面插入

    a当前光标后面插入

    shift+a 行尾插入

    shift+i 行首插入

    o下一行插入

    shift+o上一行插入

    dd 删除光标所在行

    三 Linux下第一个C程序

    vim a.c

    #include <stdio.h>
    
    int main ()
    {
        printf("hello word !
    ");
        return 0;
    }

    gcc a.c 编译得到a.out

    ./a.out 运行程序输出结果

    第四章 多文件操作

    多文件分而治之

    vim在打开一个文件的时候可以同时打开另外一个文件。在命令状态下输入:sp max.c 就是新建一个max.c文件并同时打开。

    Ctrl+w 可以在文件之间进行切换。

    :set nu 打开行号

    输入行号 按dd 剪切光标到行号的部分到剪切板。 p粘贴

    gcc hello.c max.c -o hello.out  将hell.c和max.c编译到hello.out

    头文件与函数定义分离

    gcc -c max.c -o max.o首先编译max.c

    gcc max.o hello.c 将编译好的max.o和hello.c一起编译。

    头文件只声明就行。而且不用编译。直接保留在软件系统中。

    第五章 makeFile 的编写与使用

    make工具可以将大型的开发项目分成若干个模块。使用make工具很清晰很快捷的整理源文件。

    首先查看是否安装了make工具。

    vi MakeFile //貌似都是这个命名

    # this is make file这是注释。gcc前面是一个Tab。
    # this is make file
    hello.out:max.o min.o hello.c
            gcc max.o min.o hello.c -o hello.o
    max.o:max.c
            gcc -c max.c
    min.o:min.c
            gcc -c min.c

    make 直接就执行MakeFile来编译程序。 重新执行的时候直接编译从来没有编译过的文件。

    第六章 main 函数详解

    main 函数中的 return

    main 函数完整版。

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

    gcc main.c -o main.out && ./main.out 这里的&&表示前面的执行成功之后执行后面的。如何判断第一句执行是否成功?

    echo $? 输出执行结果是否成功(返回0表示正确执行,否则是失败了。)

    这里echo输出的0是上面的return0;

    修改后:

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

    gcc main.c -o main2.out && ./main2.out 能够正常输出

    echo $? 输出结果是101 

     ./main2.out && ls 这里只能正常输出运行结果。&&后面的没有执行ls命令。

    因此return 0;不能随便写。

    main 函数中的参数

    argv是参数的个数。argc[]存放参数的内容。
    # include <stdio.h>
    
    int main( int argv,char* argc[])
    {
    
        printf("argv is %d
    ",argv);
        int i;
        for (i=0;i<argv ;i++){
            printf("argc[%d] is %s!
    ",i,argc[i]);
        }
    
        return 0;
    }

     gcc main2.c -o m4.out 编译

    eg1:     ./m4.out  执行 

    执行结果

    argv is 1
    argc[0] is ./m4.out!

    eg 2:    ./m4.out qw werer ds 执行 并且给多个参数 

    执行结果
    argv is 4
    argc[0] is ./m4.out!
    argc[1] is qw!
    argc[2] is werer!
    argc[3] is ds!

    第七章输入输出流和错误流

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

    #include <stdio.h>
    /*
     *stdio标准输入流  默认键盘
     *stdout标准输出流 显示器终端
     *stderr标准错误流
     */
    
    int main ()
    {
       // printf("please input the value a :
    ");
        fprintf(stdout,"please input the value a :
    ");
        int a ;
        //scanf("%d",&a);
        fscanf(stdin,"%d",&a);
        if(a<0){
        fprintf(stderr,"the value must >0.
    ");
        return 1;
        }
    
        return 0;
    }

    ./a.out 运行程序
    please input the value a :
    -1 输入的内容
    the value must >0

     echo $? 查看程序运行结果
    1返回错误的1

    重定向

    # include <stdio.h>
    
    int main ()
    {
        printf("input the int  value i:
     ");
        int i ,j;
        scanf ("%d",&i);
        printf("input the int  value j:
     ");
        scanf ("%d",&j);
        if(0!=j){
            printf("%d/%d=%d 
    ",i,j,i/j);
        }else{
            fprintf(stderr,"j!=0 
    ");
            return 1;
        }
        return 0;
    }

    输出重定向 ll /etc >>a.txt 追加到结尾

                   ll /etc >>a.txt 覆盖原有内容

    输入重定向 ./a.out < input.txt  将输入内容放在input.txt中

    错误流重定向 ./a.out 1>t.txt 2>f.txt 标准正确输出到t.txt    错误信息 返回2输出到f.txt

       

    第八章 管道原理及应用

    ls /etc 查看etc文件下的文件名

    ls /etc | grep ab 将命名ls /etc输出的结果输入给后一个命令。gerp是查询命令。管道|将前一个命令执行结果的输出流作为后一个命令的输入流。

    ps -e 查看进程。

    第九章 打造实用C语言小程序

     
    //avg.c
    # include <stdio.h>
    
    int main ()
    {
        int s ,n;
        scanf("%d,%d",&s,&n);
        float v;
        v =s/n;
        printf("v=%f 
    ",v);
        return 0;
    
    }
    //count.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",s,count);
        return 0;
    }

    编译之后

    ./count.out | ./avg.out 
    1
    2
    3
    0
    v=2.000000

  • 相关阅读:
    git——学习笔记(一)
    技术大牛的博客//文档
    python——周边
    数据挖掘算法:关联分析二(Apriori)
    数据挖掘算法:关联分析一(基本概念)
    Python入门5(pandas中merge中的参数how)
    Spark入门3(累加器和广播变量)
    Spark入门2(Spark简析)
    Spark入门1(以WordCount为例讲解flatmap和map之间的区别)
    算法入门1(决策树)
  • 原文地址:https://www.cnblogs.com/220l/p/4324625.html
Copyright © 2020-2023  润新知