zoukankan      html  css  js  c++  java
  • 【更新中】C语言语法汇总(仅记录遇到的坑)

    宏展开字符串

    //一般的 字符串展开宏 的定义方式
    #define STR(s) #s
    //或者,兼容STR内部宏展开的方式
    #define _STR(s) #s
    #define STR(s) _STR(s)
    
    //!!!划重点了!!!#标记的后接的参数必须只能是传进来的形参!!!
    //于是我倒腾了一晚上以为能用一个宏来展开"1.2.3.0"这样的一个字符串最终需要调用两个宏实现
    #define VERSION1 1
    #define VERSION2 2
    #define VERSION3 3
    #define VERSION4 0
    
    #define MainValueFileVersion MainValueProductVersion
    #define OtherValueFileVersion OtherValueProductVersion
    
    //我的目的是让 MainValueProductVersion 展开为 1,2,3,0 而 OtherValueProductVersion 展开为 “1.2.3.0” 
    #define MainValueProductVersion VERSION1,VERSION2,VERSION3,VERSION4 
    #define OtherValueProductVersion VERSION1.VERSION2.VERSION3.VERSION4 
    
    //最终调用方式,可以用'gcc -E 文件名'进行查看 
    STR(OtherValueProductVersion)

    结构体赋值

    typedef struct{
        int a,b,c;
    }A;
    
    A struct1 = {1,2,3};    //gnu++11支持,gnu11不支持
    A struct2 = (A){1,2,3};    //gnu++11、gnu11支持
    

     定义和使用二维数组指针

    int   a[ROW_NUM][COLUMN_NUM];
    int **p1 = a;    //XXX: expected expression
    int (*p2)[COLUMN_NUM] = a;    //correct
    

     perror的函數原型,不接受其他參數

    void perror(const char * str);
    

    如果死循环内的usleep的时间太短,一次循环历时太短,系统的使用率会迅速填满。猜测是因为usleep时间太短(之前usleep的时间是20),造成死锁。

    while(flag)
        usleep(20);  //WRONG WAY
    

    不同线程中使用malloc动态分配内存,即便是对文件全局变量进行赋值,依然不能改变指针指向的动态空间。

    代码暂时忽略
    造成原因,暂分析为malloc函数是线程安全的,造成不同线程对同一全局变量的赋值没有生效(很是想不通,希望广大网友慷慨解读)

    linux 系统下的 printf输出64整形方式(gcc 5.3 -std=gnu11 + centOS 7)

    uint64_t a = 123456789012;
    printf("a:=%llu", a);
  • 相关阅读:
    【Leetcode】【Easy】Remove Duplicates from Sorted List
    【Leetcode】【Easy】Pascal's Triangle II
    【Leetcode】【Easy】Pascal's Triangle
    【Leetcode】【Easy】Binary Tree Level Order Traversal II
    【Leetcode】【Easy】Binary Tree Level Order Traversal
    【Leetcode】【Easy】Maximum Depth of Binary Tree
    【Leetcode】【Easy】Minimum Depth of Binary Tree
    【Leetcode】【Easy】Balanced Binary Tree
    【Leetcode】【Easy】Symmetric Tree
    如何使用Action.Invoke()触发一个Storyboard
  • 原文地址:https://www.cnblogs.com/pureLaw/p/6925705.html
Copyright © 2011-2022 走看看