AlgebraMaster

Modern C++ 创造非凡 . 改变世界

导航

Linux C 学习

 

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

    scanf("%f",&f_test);
    printf("%f\n",f_test);

}


格式输出浮点

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

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

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

    int index = 0 ;
    while(++index < 5)
    {
        printf("%5d \n",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\n",i,_data[i] );
    }
}

void get_name()
{
    printf("name is gearslogy\n");
}


void run_get_name(void (*pf)()) // argument point to a function address
{
    pf();
    printf("function address %p\n",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 .\n")
int main()
{
    printf("Running ps with execlp\n");
    execlp("ps","ps","ax",0);
    printf(" Done.\n");
    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\n");
    pid = fork();
    switch (pid)
    {
        case -1:
            perror("fork error\n");
            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 \n",child_pid);
        if(WIFEXITED(stat_val))
        {
            printf("Child exit with code %d\n",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 \n",sig);
}
int main()
{
    signal(SIGINT,ouch);
    while(1)
    {
        printf("hello world\n");
        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\n");

    pid = fork();
    switch(pid)
    {
    case -1:
        perror("fork faild\n");
        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\n");
    signal(SIGALRM,ding); // main process recive the signal
    pause();
    if(arlarm_fired)
    {
        printf("ding ding\n");
    }
    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;
}

 

posted on 2016-04-18 22:10  gearslogy  阅读(579)  评论(0编辑  收藏  举报