linux学习demo--6.20-7.3

学习linux过程中写的demo,随时可以进行复习

/*
c demo

*/

//常用的头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>

//系统程序调用
static int demo1()
{
    printf("system before\n");
    system("ls");
    printf("system after\n");
    return 0;
}

//调用外部程序
static int demo2()
{
    printf("system before\n");
    system("../test/a.out");
    printf("system after\n");
    return 0;
}

//汇编运算的小demo
//未能运行,后续继续寻找资料操作
#if 0
static int demo3()
{
    int a,b,c;

    __asm__
    (
        "movl $a, $3"
        "movl $b, $4\n\t"
        "movl %eax, a\n\t"
        "addl %eax, b\n\t"
        "movl c, %eax\n\t"
        :
        :
        :
    );
    

    printf("the end a+b = c is %d\n", c);
    return 0;
}
#endif

// extern 声明变量的关键字,但是这个值没有定义,就没有存储空间,直接赋值就会报错
static int demo4()
{
    //此种写法错误
    // extern int a;
    int a;
    a = 10;
    return 0;
}

//八进制,十六进制打印,八进制以0开头,十六进制以0x开头,
void demo5()
{
    int a = 12;
    int b = 0xA;
    printf("十进制: %d\n", b);
    printf("十进制: %d\n", a);
    printf("八进制: %o\n", a);
    printf("十六进制: %x\n", a);
}

//验证两个十六进制数的输出
void demo6()
{
    char a = 0x81;
    /*
    计算机的存储
    补码:1000 0001
    反码:1111 1110
    原码:1111 1111
    就是 -127
    */
    printf("十六进制输出一个十进制为 %d\n", a);
    int b = (int)a;
    char c = (char)b;
    printf("十六进制的数字转化成char型: %d\n", c);
    /*
    试试
    1110 0101
    反:1001 1010
    原码: 1001 1011
    %d: -(16+8+2+1) = -27
    0xe5
    */
    char d = 0xe5;
    printf("the 0xe5 is %d \n", d);

    /*
    试试0110 1111
    = 64+32+15 = 111
    0x3f
    */
    char e = 0x6f;
    printf("0x3f is %d \n", e);

    /*
    10进制数,在用户的角度
    int a = -123;
    原码: 1000 0000 0000 0000 0000 0000 0111 1011
    反码: 1111 1111 1111 1111 1111 1111 1000 0100
    补码: 1111 1111 1111 1111 1111 1111 1000 0101
          f     f    f    f     f   f    8    5
    */
    char f = -123;
    printf("-123 四个字节输出: %x \n", f);

    /*
     0x50
     0101 0000
    */
}

/*模拟网络端的传输,用最节省的方式传输int 30000,存此数字只需要16位,就使用两个char进行发送,接收端也使用这个进行解析字符串*/
void demo7()
{
    /*
   发送int 30000
   */
    char sendC = 'K';
    int sendInt = sendC - '0';
    char sendChar[2];
    sendChar[0] = (char)sendInt;
    sendChar[1] = (char)((sendInt) >> 8);
    //发送出去,接收端将其复原
    int lowInt = (int)sendChar[0];
    int hightInt = (int)sendChar[1];
    int wholeInt = ((hightInt) << 8) + lowInt;
    printf("%d\n", wholeInt);
    char getC = '0' + wholeInt;
    printf("%c \n", getC);
}

/*模拟网络传输的另外一种不需要移位进行的操作*/
void demo8()
{
    union ci
    {
        int i;
        char c[4];
    } uci;
    int i = 0;
    union ci sendNum;
    sendNum.i = 30000;

    //模拟收到数据的过程
    char getChar[4];
    strncpy(getChar, sendNum.c, sizeof(getChar));
    union ci getNum;
    strncpy(getNum.c, getChar, sizeof(getNum.c));

    printf("get num is %d\n", getNum.i);
}

/*
二进制输出char
1移位到要比较的位置,然后进行&运算,将得到的数字右移回来得到比较的值
*/
void ctob(char chr)
{
    char tmpchr = chr;
    int i = 0;
    for (i = 7; i >= 0; i--)
    {
        char outchr;
        outchr = tmpchr & (1 << i);
        printf("%d", (outchr >> i));
    }
    printf("\n");
}

/*
二进制输出char
1移位到要比较的位置,然后进行&运算,将得到的数字右移回来得到比较的值
*/
void ctob2(char chr)
{
    char tmpchr = chr;
    int i = 0;
    for (i = 7; i >= 0; i--)
    {
        char outchr;
        outchr = tmpchr & (1 << i);
        if (7 == i)
        {
            if (outchr == 0x80)
            {
                printf("1");
            }
            else
            {
                printf("0");
            }
            continue;
        }
        if (0x1 == outchr)
        {
            printf("1");
        }
        else
        {
            printf("0");
        }
    }
    printf("\n");
}

//有符号和无符号的区别
void demo9()
{
    /*
    测试在计算机中的一串数字 ,因为是一个负数,所以这个数字就是一个补码
    1000 0000 0000 0000 0000 0000 0111 1011
    1111 1111 1111 1111 1111 1111 1000 0100
    1111 1111 1111 1111 1111 1111 1000 0101
    7    f    f    f    f    f    8    5
    = -2,147,483,525
    无符号的打印就是忽略符号位,并不是把符号位的数当成一个数字
    */
    //有符号的方式打印
    printf("%d\n", 0x8000007b);
    //无符号的方式打印
    printf("%u\n", 0x8000007b);
    //有符号的方式打印无符号数字,会有负号,因为打印的解释就是带着负号
    unsigned int c = 0x8000007b;
    printf("%d\n", c);
    unsigned char a = 0xff;
    printf("unsigned char is %d \n", a);
    printf("----------sss----------\n");
    char schr = -127;
    ctob2(schr);
}

// char越界
void demo10()
{
    char a = 127 + 2;
    printf("a is %d\n", a);
    printf("sizeof int is %d\n", sizeof(int));
    printf("sizeof char is %d\n", sizeof(char));
}

//输入输出
void demo11()
{
    int a;
    printf("输入a的值: ");
    scanf("%d", &a);
    printf("a is %d \n", a);

    // short 是两个字节
    short b;
    printf("请输入b :");
    scanf("%hd", &b);
    printf("the b is %hd\n", b);

    // long long 是八个字节
    long long c;
    printf("请输入c :");
    scanf("%lld", &c);
    printf("the c is %lld\n", c);
}

//测试ascii码
void demo12()
{
    char a = '\n';
    printf("fffff%c", a);
    char ch = '\r'; //句首
    printf("aaaaaaaaaaaa%cbbbbb\n", ch);
    char ch1 = '\t';
    printf("%caaaaaaaaaaaabbbbb\n", ch1);
    printf("\x42\n");
    int ch_B = 0x42;
    printf("%c\n", ch_B);
}

//测试float
void demo13()
{
    float f = 100.2;
    printf("%f", f);
}

// printf demo
void demo14()
{
    int a = 5;
    printf("%015d\n", a);
    putchar('a');
    putchar('\n');
}

// scanf的使用
void demo15()
{
    char a, b, tmp;
    scanf("%c", &a);
    printf("a is %c\n", a);
    //吃掉上一次的\n
    scanf("%c", &tmp);
    scanf("%c", &b);
    printf("b int is %d\n", b);
    printf("b char is %c\n", b);

    printf("printf a double ..: \" \n");
    printf("printf a single ..: ' \n");
}

//前置后置
void demo16()
{
    int a = 11;
    printf("a is %d\n", a++);
    printf("a is %d\n", a);
    printf("a is %d\n", ++a);
    printf("a is %d\n", a);
    int b;
    a = 1;
    b = ++a;
    printf("b is %d\n", b);
    int c;
    a = 1;
    c = a++;
    printf("c is %d\n", c);
}

//&& || demo
static int trueFalseTest()
{
    printf("do!\n");
    return 0;
}
void demo17()
{
    printf("true or false? %d\n", 1 || trueFalseTest());
    printf("true or false? %d\n", 0 && trueFalseTest());
    printf("true or false? %d\n", -1 && 0);
}

//数组地址的计算
void demo18()
{
    char a[10] = {0};
    int i = 0;
    int start = 3;
    for (i = 0; i < 10; i++)
    {
        a[i] = start++;
    }
    printf("%x\n", &a[0]);
    printf("%x\n", &a[1]);
    printf("%x\n", &a[2]);
    printf("%x\n", &a[3]);
    printf("%x\n", &a[4]);

    ctob(a[0]);
    char *ptr = &a[0];
    printf("the pointer ptr is %x\n", ptr);

    ptr++;
    *ptr = 0x34;
    //按位异或 demo
    char tmpptr = *ptr;
    *ptr = (1 << 6) ^ tmpptr;
    printf("the pointer ptr is %x\n", ptr);
    printf("the pointer ptr value is %x\n", *ptr);
    printf("the pointer ptr character is %c\n", *ptr);
    ctob(*ptr);
}

//三目运算符demo
void demo19()
{
    printf("the end is %d\n", (3 > 2) ? 30 : 20);
}

//不初始化的数组
void demo20()
{
    int a[10];
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        printf("a is %d\n", a[i]);
    }
    printf("the addr is %p\n", a);
    printf("the addr is %p\n", &a[0]);
    printf("the addr is %p\n", &a[1]);
}

//二维数组

void printName(int nameC, char **name)
{
    int i = 0;
    for (i = 0; i < nameC; i++)
    {
        printf("strlen is : %d\n", strlen(name[i]));
        printf("str is : %s, %dth char is : %c\n", name[i], i, name[i][0]);
    }
}

void demo21()
{
    int i, j;
    //初始化,两种
    int a[3][4] =
        {
            {0, 1, 2, 3},
            {4, 5, 6, 7},
            {8, 9, 10, 11},
        };
    int a1[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("the hex addr is %x\n", &a[i][j]);
            printf("the p addr is %p\n", &a[i][j]);
        }
    }
}

void demo22()
{
    char *name[4] = {"liangxin", "liming", "lihua", "xiaoli"};

    printName(4, (char **)name);

    /*
    printf("str is : %s, %dth char is : %c\n", name[0], 0, name[0][0]);
    printf("%p\n", &name[0][1]);
    printf("%p\n", &name[1][1]);
    printName((char **)name);
    */
}

//字符数组乱码
void demo23()
{
    char a[10] = {'a', 'b', 'c'};
    printf("a = %s\n", a);
}

//产生随机数demo
void demo24()
{
    int i;
    srand(time(NULL));
    for (i = 0; i < 10; i++)
    {
        int num = rand() % 100;
        printf("the srand is %d\n", num);
    }
}

// scanf输入字符串demo
void demo25()
{
    char buf[1024];
    scanf("%s", buf);
    printf("out is %s\n", buf);
    scanf("%s", buf);
    printf("out is %s\n", buf);
}

// gets demo
void demo26()
{
    char buf[1024];
    fgets(buf, 1024, stdin);
    printf("out is : %s\n", buf);
}

// fgets demo
void demo27()
{
    char buf[3];
    fgets(buf, sizeof(buf), stdin);
    printf("buf = %s\n", buf);
}

// puts fputs
void demo28()
{
    char buf[] = "i am liangxin";
    puts(buf);
    printf("test\n");
    fputs(buf, stdout);
}

// strlen demo
void demo29()
{
    int i;
    char buf[] = "aabb\0aabb";
    char buf1[] = "aabb\naabb";
    printf("lenth is %d\n", strlen(buf));
    printf("lenth is %d\n", strlen(buf1));
    char buf2[] = "aabb";
    char buf3[] = "aabb";
    for (i = 0; i < 7; i++)
    {
        printf("x is : %x\n", buf[i]);
    }
    printf("sizeof lenth is %d\n", sizeof(buf));
    printf("sizeof lenth is %d\n", sizeof(buf1));
}

// strcpy strncpy demo
void demo30()
{
    char src[100] = "hello\0 mike";
    char dst[100];

    strcpy(dst, src);
    printf("out is :%s\n", dst);

    memset(dst, 0, sizeof(dst));
    strncpy(dst, src, 12);
    printf("out is :%s\n", dst + strlen("hello") + 1);
}

// strcpy 段错误
void demo31()
{
    char src[] = "aaaaaaaaaaaaaaaaaa";
    char dst[3];
    strcpy(dst, src);
}

// strcmp的使用,加上n就是比较前多少个
void demo32()
{
    char src[] = "abc";
    char dst[] = "Abc";
    printf("the cmp out is :%d\n", strcmp(src, dst));
}

//字符串追加
void demo33()
{
    char src[] = "hello";
    char dst[] = "liangxin";
    // strcat(src, dst);
    strncat(src, dst, 2);
    printf("%s\n", src);
}

// sscanf 使用demo,注意提取字符串使用空格分隔
void demo34()
{
    char src[] = "a=10, b=20";
    int a;
    int b;
    sscanf(src, "a=%d, b=%d", &a, &b);
    printf("a:%d, b:%d\n", a, b);
    char str1[1024];
    char str2[1024];
    sscanf(src, "%s, %s", str1, str2);
    printf("str is :%s\n", str1);
    printf("str is :%s\n", str2);
}

// strchr 使用
void demo35()
{
    char src[] = "hello liangxin";
    char *p = strchr(src, 'i');
    if (NULL == p)
    {
        printf("fail\n");
    }
    else
    {
        printf("p = %s\n", p);
    }
}

// strstr 使用
void demo36()
{
    char src[] = "hello liangxin";
    char *p = strstr(src, "ian");
    if (NULL == p)
    {
        printf("fail\n");
    }
    else
    {
        printf("p = %s\n", p);
    }
}

// strsok 使用
void demo37()
{
    char src[] = "hello,liang,xin";
    char *p = strtok(src, ",");
    while (NULL != p)
    {
        printf("p = %s\n", p);
        p = strtok(NULL, ",");
    }
}

//atoi demo
void demo38()
{
    char str[] = "192";
    int addr = atoi(str);
    printf("int addr is %d\n", addr);
}

int main(int argc, char *argv[])
{
    demo38();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值