zoukankan      html  css  js  c++  java
  • Linux C 学习

    int main()
    {   
        int64_t test = 100;
        printf("%lld
    ",test);
        float f_test = 100.2123;
        printf("%f
    ",f_test);
    
        scanf("%f",&f_test);
        printf("%f
    ",f_test);

    }


    格式输出浮点

        double shoes = 10.23;
        printf("%-10.2f ok
    ",shoes);
        printf("%-15.2f ok
    ",shoes);
        printf("%10.2f ok
    ",shoes);
        printf("%15.2f ok
    ",shoes);
        printf("%.2f ok
    ",shoes);
        printf("%.6f ok
    ",shoes);
        printf("%.2ef ok
    ",shoes);

    整数空格输出 ,下面控制前面5个空格

    如果是%-5d,就是右对齐。上面的float输出也是一样的

        int index = 0 ;
        while(++index < 5)
        {
            printf("%5d 
    ",index);
        }
        return 0;

    #include <stdio.h>
    void test(const void *data,int size)
    {
        const float *_data = (float*)data;
        for(int i=0;i<size;i++)
        {
            printf("data array %d is %f
    ",i,_data[i] );
        }
    }
    
    void get_name()
    {
        printf("name is gearslogy
    ");
    }
    
    
    void run_get_name(void (*pf)()) // argument point to a function address
    {
        pf();
        printf("function address %p
    ",pf);
    }
    
    
    int main()
    {
        float a[4] = {1,2,3,4};
        test(a,4);
    
        run_get_name(get_name);
        return 0;
    }

    进程fork:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    /*
    
     // THIS PROGRAM WILL REPLACE THE MAIN(),SO WILL DO NOT RUN THE printf(" Done .
    ")
    int main()
    {
        printf("Running ps with execlp
    ");
        execlp("ps","ps","ax",0);
        printf(" Done.
    ");
        return 0;
    }
     */
    
    // wait
    #include <sys/wait.h>
    #include <stdlib.h>
    
    int main()
    {
        pid_t  pid;
        char *message;
        int n;
        int exit_code;
    
        printf("fork program starting
    ");
        pid = fork();
        switch (pid)
        {
            case -1:
                perror("fork error
    ");
                exit(1);
            case 0:
            {
                message = "this is the child";
                n=5;
                exit_code = 10; // child return code
                break;
            }
            default:
            {
                message = "This is the parent";
                n=3;
                exit_code = 55;  // this is the main process exit_code
                break;
            }
        }
    
        for(;n>0;n--)
        {
            puts(message);
            sleep(1);
        }
    
        if(pid != 0 )
        {
            int stat_val;
            pid_t child_pid;
            child_pid = wait(&stat_val); //return the child pid
            printf("child has finished : PID=%d 
    ",child_pid);
            if(WIFEXITED(stat_val))
            {
                printf("Child exit with code %d
    ",WEXITSTATUS(stat_val));
            }
        }
        exit(exit_code); // this is the child exit code,this will eval two times,becuase have a main process,and fork() process
    
    }
    View Code

    信号:

    void ouch(int sig)
    {
        printf("I got signal %d 
    ",sig);
    }
    int main()
    {
        signal(SIGINT,ouch);
        while(1)
        {
            printf("hello world
    ");
            sleep(1);
        }
        return 0;
    }
    View Code

     信号等待获取:

    static int arlarm_fired = 0;
    void ding(int sig)
    {
        arlarm_fired = 1;
    }
    // in the child process wait 5 secends,then send a signal
    // to the father process
    int main()
    {
        pid_t pid;
        printf("alarm application starting
    ");
    
        pid = fork();
        switch(pid)
        {
        case -1:
            perror("fork faild
    ");
            exit(1);
        case 0:
            sleep(5);
            kill(getppid(),SIGALRM); // send a signal to main(father)  process
            exit(0);
        }
    
        printf("waiting for alarm to go off
    ");
        signal(SIGALRM,ding); // main process recive the signal
        pause();
        if(arlarm_fired)
        {
            printf("ding ding
    ");
        }
        exit(0);
    }
    View Code

    简单的二维数组

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int zippo[2][3] = {{1,2,3},
                           {4,5,6}};
        cout << zippo[0] << endl;  // point to the {1,2,3}
        cout << zippo[1] << endl;  // point to the {4,5,6}
    
        cout << (zippo+0) <<endl;  // point to the {1,2,3}
        cout << (zippo+1) <<endl;  // point to the {4,5,6}
    
        cout << *(*zippo+0) <<endl;  // -> 1
        cout << *(*zippo+1) <<endl;  // -> 2
    
        cout << *(*(zippo+1)+0) <<endl;  // ->4
        cout << *(*(zippo+1)+1) <<endl;  // ->5
        return  0;
    }
  • 相关阅读:
    [C# 基础知识系列]专题六:泛型基础篇——为什么引入泛型
    [C#基础知识系列]专题十七:深入理解动态类型
    [C# 基础知识系列]专题九: 深入理解泛型可变性
    C#网络编程系列文章索引
    [C#基础知识系列]专题十:全面解析可空类型
    [C# 基础知识系列]专题十一:匿名方法解析
    [C# 基础知识系列]专题十六:Linq介绍
    VSTO之旅系列(一):VSTO入门
    [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
    [C# 网络编程系列]专题四:自定义Web浏览器
  • 原文地址:https://www.cnblogs.com/gearslogy/p/5406130.html
Copyright © 2011-2022 走看看