您当前的位置: 首页 > 

Jave.Lin

暂无认证

  • 1浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C - Book Note

Jave.Lin 发布时间:2020-04-23 14:17:03 ,浏览量:1

文章目录
  • Win 开发环境设置
  • Hello world!
  • printf format test
  • define variable test
  • extern var test
  • time interval test
  • time detail test
    • common.h
    • asctime test
    • clock per second test
    • clock test
    • ctime test
    • difftime test
    • gmtime test
  • applied domain test
  • 1D array test
  • multi D array test
  • enum test
  • pointer test
  • rand test
  • callback or func pointer test
  • string test
  • struct test
  • union test
  • stdio test
  • file io test
  • pre-process or macro test
  • include test
    • include each other
      • 使用gcc -E输出只有预处理后的内容
      • 解决方法
        • 将共用变量提取到一个global.h
        • 添加条件编译来include
  • error print test
  • dynamic arg num test
  • func variables heap enqueue test
  • mem mgr test
  • call app pass args test
  • doublePointer test / 二级、多级指针测试
    • 二级指针测试
  • assert test
  • float frexp test
  • long double 如何输出
  • signal handle test
  • convert str to number test
  • offsetof test
  • atexit test
  • getenv test - 获取环境变量值
  • system test
    • windows powershell or cmd直接输入指令是没有问题的
  • gcc size tools
  • setjmp, longjmp test
    • 在单个函数内执行跳转
    • 在单个函数重复执行
    • 多个方法之间的跳转
  • struct initialize test
  • References
出于某些原因,全面复习一下C还是有必要的。

那就让我们快速复习、记录一下吧。

后面接着转战C++

Win 开发环境设置

C 环境设置

  • 在 MinGW 下载对应的GCC
    • 下载MinGW Installation Manager
    • 选择安装GCC bin相关的就好
    • 其他的功能暂不需要
  • IDE 我使用的是Visual Studio Code,安装上C/C++插件就可以撸代码了
Hello world!
#include 
#include 

int main() {

    printf("hello world! \n");
    system("pause");

    return 0;
}

运行输出:

PS D:\jave\Work Files\C\Studies\1 hello world> gcc .\hello_world.c -o out
PS D:\jave\Work Files\C\Studies\1 hello world> .\out.exe
hello world!
printf format test
#include 
#include 
#include 

int main() {

    int size = 5555;
    unsigned int usize = ((unsigned int)-1) - UINT_MAX;
    float fsize = 555.123456;
    long double ldsize = 555.123456;
    char c = 'm';
    char str[20] = "this is your name";
    int* p = &size;
    int negNum = -1;
    printf("d       : %d       \n",            size);    // d       : 5555
    printf("000d    : %000d    \n",            size);    // 000d    : 5555
    printf("###d    : %###d    \n",            size);    // ###d    : 5555
    printf("--d     : %--d     \n",            size);    // --d     : 5555
    printf("++d     : %++d     \n",            size);    // ++d     : +5555
    printf("   d    : %   d    \n",            size);    //    d    :  5555
    printf("3d      : %3d      \n",            size);    // 3d      : 5555
    printf("6d      : %6d      \n",            size);    // 6d      :   5555
    printf("06d     : %06d     \n",            size);    // 06d     : 005555
    printf("ld      : %ld      \n",            size);    // ld      : 5555
    printf("o       : %o       \n",            size);    // o       : 12663
    printf("x       : %x       \n",            size);    // x       : 15b3
    printf("X       : %X       \n",            size);    // X       : 15B3
    printf("0X      : %0X      \n",            size);    // 0X      : 15B3
    printf("u       : %u       \n",            usize);   // u       : 0
    printf("#X      : %#X      \n",            size);    // #X      : 0X15B3
    printf("#x      : %#x      \n",            size);    // #x      : 0x15b3
    printf("f       : %f       \n",            fsize);   // f       : 555.123474
    printf("e       : %e       \n",            fsize);   // e       : 5.551235e+002
    printf("E       : %E       \n",            fsize);   // E       : 5.551235E+002
    printf("g       : %g       \n",            fsize);   // g       : 555.123
    printf("G       : %G       \n",            fsize);   // G       : 555.123
    printf(".2f     : %.2f     \n",            fsize);   // .2f     : 555.12
    printf(".3e     : %.3e     \n",            fsize);   // .3e     : 5.551e+002
    printf(".4E     : %.4E     \n",            fsize);   // .4E     : 5.5512E+002
    printf(".5g     : %.5g     \n",            fsize);   // .5g     : 555.12
    printf(".6G     : %.6G     \n",            fsize);   // .6G     : 555.123
    printf(".*f     : %.*f     \n",            fsize);   // .*f     : 0.000000
    printf(".*e     : %.*e     \n",            fsize);   // .*e     : 5.346871e-315
    printf(".*E     : %.*E     \n",            fsize);   // .*E     : 5.346871E-315
    printf(".*g     : %.*g     \n",            fsize);   // .*g     : 5.34687e-315
    printf(".*G     : %.*G     \n",            fsize);   // .*G     : 5.34687E-315
    printf("p       : %p       \n",            p);       // p       : 0061FEFC
    printf("d       : %d       \n",            *p);      // d       : 5555
    printf("c       : %c       \n",            c);       // c       : m
    printf("s       : %s       \n",            str);     // s       : this is your name
    printf("u       : %u       \n",            negNum);  // u       : 4294967295
    printf("lu      : %lu      \n",            negNum);  // lu      : 4294967295
    printf("llu     : %llu     \n",            negNum);  // llu     : 4648094134073032703
    system("pause");

    return 0;
}


PS D:\jave\Work Files\C\Studies\2 printf_format> gcc .\printf_format.c -o out                                           PS D:\jave\Work Files\C\Studies\2 printf_format> .\out.exe                                                              d       : 5555
000d    : 5555
###d    : 5555
--d     : 5555
++d     : +5555
   d    :  5555
3d      : 5555
6d      :   5555
06d     : 005555
ld      : 5555
o       : 12663
x       : 15b3
X       : 15B3
0X      : 15B3
u       : 0
#X      : 0X15B3
#x      : 0x15b3
f       : 555.123474
e       : 5.551235e+002
E       : 5.551235E+002
g       : 555.123
G       : 555.123
.2f     : 555.12
.3e     : 5.551e+002
.4E     : 5.5512E+002
.5g     : 555.12
.6G     : 555.123
.*f     : 0.000000
.*e     : 5.346871e-315
.*E     : 5.346871E-315
.*g     : 5.34687e-315
.*G     : 5.34687E-315
p       : 0061FEFC
d       : 5555
c       : m
s       : this is your name
u       : 4294967295
lu      : 4294967295
llu     : 4648094134073032703
define variable test
#include 
#include 

int main() {
    
    float f_var = 210.0f;
    double ff_var = 2.123456789E+10;
    printf("f_var=%.2f\n", f_var);
    printf("ff_var=%.2e\n", ff_var);
    system("pause");

    return 0;
}
extern var test

a.c

#include 

extern int a;
extern int b;
int c;
extern int d;

int sum() {
    return a+b+c+d;
}

int main() {
    printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

    printf("a+b=%d\n",a+b);
    printf("c+a=%d\n",c+a);
    printf("c+d=%d\n",c+d);
    printf("sum()=%d\n",sum());
    return 0;
}

b.c

int a = 1;
int b = 2;

extern int c;

int d = 3;
PS D:\jave\Work Files\C\Studies\extern var> gcc .\a.c .\b.c -o out
PS D:\jave\Work Files\C\Studies\extern var> .\out.exe
a=1,b=2,c=0,d=3
a+b=3
c+a=1
c+d=3
sum()=6
time interval test
#include 
#include 

#define TIME 1000000000
int m, n = TIME; /* 全局变量 */

int main(void)
{   
    time_t start, stop;
    register int a, b = TIME; /* 寄存器变量 */
    int x, y = TIME;          /* 一般变量   */

    time(&start);
    for (a = 0; a tm_min)
    LINE_F(East time : %2d:%02d, (curtm->tm_hour + CCT_DIFF_HOURS) % 24, curtm->tm_min)

    /*
    current world time :
    West time : 11:10
    East time : 18:10
    */

    return 0;
}
applied domain test
#include 

void func(int a) {
    {
        int a = 4;
        printf("-->a=%d\n",a);
    }
    printf("->a=%d\n",a);
}

int main() {

    int a = 1;
    {
        int a = 2;
        printf("-->a=%d\n",a);
    }
    printf("->a=%d\n",a);

    func(3);

    return 0;
}
PS D:\jave\Work Files\C\Studies\applied domain> gcc .\a.c -o out
PS D:\jave\Work Files\C\Studies\applied domain> .\out.exe
-->a=2
->a=1
-->a=4
->a=3
1D array test
#include 

int main() {

    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    const int len = sizeof(arr) / sizeof(int);
    printf("len=%d\n", len);
    for (int i = 0; i arr, size);
}

void FreeArr(ARR_STRUCT v) {
    free(v.arr);
}

int main() {

    int arr[10][10];

    int len2 = sizeof(arr[0]) / sizeof(int);
    int len1 = sizeof(arr) / (sizeof(int) * len2);

    printf("sizeof(arr) = bytes =%d\n", sizeof(arr));

    printf("len1=%d\n", len1);
    printf("len2=%d\n", len2);

    int idx = 0;
    for (int row = 0; row name,"1");
    strcpy(b_pointer->name,"2");
    printf("after free a_pointer->name=%s,b_struct.name=%s\n",a_pointer->name,b_pointer->name);

    // 所以我们最好养成习惯free了之后的heap数据都指向NULL
    a_pointer = NULL;
    b_pointer = NULL;
    // 这时输出地址就变成00000000了
    printf("after free a_pointer=%p,b_pointer=%p\n",a_pointer,b_pointer);
    // 如果恢复下面对NULL数据的->name就会报错
    // strcpy(a_pointer->name,"1");
    // strcpy(b_pointer->name,"2");

    printf("sizeof(unsigned)==%d\n", sizeof(unsigned)); // 4
    printf("sizeof(unsigned int)==%d\n", sizeof(unsigned int)); // 4
    printf("sizeof(unsigned long)==%d\n", sizeof(unsigned long)); // 4
    printf("sizeof(long int)==%d\n", sizeof(long int)); // 4
    printf("sizeof(unsigned long int)==%d\n", sizeof(unsigned long int)); // 4
    printf("sizeof(long double)==%d\n", sizeof(long double)); // 12
    printf("sizeof(double)==%d\n", sizeof(double)); // 8

    // bit field testing
    printf("=====bit field testing=====\n");

    BFType bf = {1,1,1,1,1,1,1,1};
    printf("sizeof(BFType)==%d\n",sizeof(BFType));
    printf("sizeof(bf)==%d\n",sizeof(bf));
    printf("bf.v1=%d\n",bf.v1);
    printf("bf.v2=%d\n",bf.v2);
    printf("bf.v3=%d\n",bf.v3);
    printf("bf.v4=%d\n",bf.v4);
    printf("bf.v5=%d\n",bf.v5);
    printf("bf.v6=%d\n",bf.v6);
    printf("bf.v7=%d\n",bf.v7);
    printf("bf.v8=%d\n",bf.v8);

    /*
    sizeof(BFType)==8
    sizeof(bf)==8
    bf.v1=-1
    bf.v2=1
    bf.v3=-1
    bf.v4=1
    bf.v5=-1
    bf.v6=1
    bf.v7=-1
    bf.v8=1

    因为有些只剩一个符号位了,所以就是-1,
    所以一般只有1 bit的,一般用无符号来表示,结果就是0或是1
    */

    printf("done!\n");
    return 0;
}
PS D:\jave\Work Files\C\Studies\struct test> gcc .\a.c -o out
PS D:\jave\Work Files\C\Studies\struct test> .\out.exe
sizeof(AType)==56,sizeof(BType)==56
=====on stack variable testing=====
a_struct.name=This is A's name,b_struct.name=This is B's name
a_struct=0061FEE0,b_struct=0061FEA8
a_struct.b_pointer=0061FEA8,b_struct.a_pointer=0061FEE0
a_struct.name=Tom,b_struct.name=Jerry
=====on heap pointer variable testing=====
a_pointer->name=Bill.Gates,b_struct.name=Steven.Jobs
a_pointer=00C022D8,b_pointer=00C02318
a_pointer->b_pointer=00C02318,b_pointer->a_pointer=00C022D8
free the pointers
after free a_pointer=00C022D8,b_pointer=00C02318
after free a_pointer->name=1,b_struct.name=2
after free a_pointer=00000000,b_pointer=00000000
sizeof(unsigned)==4
sizeof(unsigned int)==4
sizeof(unsigned long)==4
sizeof(long int)==4
sizeof(unsigned long int)==4
sizeof(long double)==12
sizeof(double)==8
=====bit field testing=====
sizeof(BFType)==8
sizeof(bf)==8
bf.v1=-1
bf.v2=1
bf.v3=-1
bf.v4=1
bf.v5=-1
bf.v6=1
bf.v7=-1
bf.v8=1
done!
union test
#include 

typedef struct S1 {
    char v1;
    char v2;
} S1Type;

typedef struct S2 {
    char v1;
    char v2;
    short v3;
    int v4;
} S2Type;

typedef struct S3 {
    char v1;
    short v3;
    char v2;
    int v4;
} S3Type;

typedef union U1 {
    char v1;
} U1Type;

typedef union U2 {
    char v1;
    short v2;
} U2Type;

typedef union U3 {
    char v1;
    short v2;
    unsigned v3;
} U3Type;

typedef union U4 {
    char v1;
    short v2;
    unsigned v3;
    double v4;
} U4Type;

typedef union U5 {
    char v1;
    short v2;
    unsigned v3;
    double v4;
    long double v5;
} U5Type;

typedef union U6 {
    char v1;
    U1Type v2;
    S1Type v3;
} U6Type;

typedef union U7 {
    char v1;
    S2Type v2;
} U7Type;

typedef union U8 {
    char v1;
    S3Type v2;
} U8Type;

typedef union U9 {
    char v1;
    char v2[20];
    double v3;
    // sizeof(char) == 1 && sizeof(double) == 8 : S
    // 以最大double 8 bytes对齐
    // max(sizeof(v1), sizeof(v2), sizeof(v3)) == 20 : M
    // sizeof(U9Type):
    // == ((int)(M / S)  + (S % M  != 0 ? 1 : 0)) * S
    // == ((int)(20 / 8) + (8 % 20 != 0 ? 1 : 0)) * 8
    // == (      2       + (  4    != 0 ? 1 : 0)) * 8
    // == (      2       + (       1           )) * 8
    // == (              3                      ) * 8
    // == 3 * 8 = 24
} U9Type;

int main() {

    printf("sizeof(U1)==%d\n",sizeof(U1Type));
    printf("sizeof(U2)==%d\n",sizeof(U2Type));
    printf("sizeof(U3)==%d\n",sizeof(U3Type));
    printf("sizeof(U4)==%d\n",sizeof(U4Type));
    printf("sizeof(U5)==%d\n",sizeof(U5Type));
    printf("sizeof(U6)==%d\n",sizeof(U6Type));
    printf("sizeof(U7)==%d\n",sizeof(U7Type));
    printf("sizeof(U8)==%d\n",sizeof(U8Type));
    printf("sizeof(U9)==%d\n",sizeof(U9Type));

    return 0;
}
PS D:\jave\Work Files\C\Studies\union test> gcc .\a.c -o out
PS D:\jave\Work Files\C\Studies\union test> .\out.exe
sizeof(U1)==1
sizeof(U2)==2
sizeof(U3)==4
sizeof(U4)==8
sizeof(U5)==16
sizeof(U6)==2
sizeof(U7)==8
sizeof(U8)==12
sizeof(U9)==24
stdio test
  • a.c
#include 
#include 

int main() {

    pr intf("Enter a char, please : ");
    char c = getchar(); // 只会取第一次输入的字符
    // printf("\n");
    printf("I got it, you enter the char is : %c\n", c);
    printf("putchar(c):");
    putchar(c);
    printf("\n");

    return 0;
}
PS D:\jave\Work Files\C\Studies\stdio test> gcc .\a.c -o out
PS D:\jave\Work Files\C\Studies\stdio test> .\out.exe
Enter a char, please : j
I got it, you enter the char is : j
putchar(c):j
  • b.c
#include 
#include 

int main() {

    char msg[100];
    printf("Enter a message again please : (limit 100 chars)");
    gets(msg);
    printf("I got your message : ");
    puts(msg);
    printf("\n");

    return 0;
}
PS D:\jave\Work Files\C\Studies\stdio test> gcc .\b.c -o out
PS D:\jave\Work Files\C\Studies\stdio test> .\out.exe
Enter a message again please : (limit 100 chars)this is message.
I got your message : this is message.

  • c.c
#include 
#include 

int main() {

    char msg[100];
    printf("Enter a message please : (limit 100 chars, split by space)");
    scanf("%s", msg);
    printf("I got your message : %s\n", msg);

    return 0;
}
PS D:\jave\Work Files\C\Studies\stdio test> gcc .\c.c -o out
PS D:\jave\Work Files\C\Studies\stdio test> .\out.exe
Enter a message please : (limit 100 chars, split by space)first second thrid
I got your message : first
file io test

先在创建tmp目录

/*
 C 文件读写
https://www.runoob.com/cprogramming/c-file-io.html
*/

#include 
#include 
#include 

int main() {
    FILE *fp = NULL;

    char *filename = "D:\\jave\\Work Files\\C\\Studies\\file io test\\tmp\\test.txt";

    fp = fopen(filename, "a+");
    char *inputStr = malloc(255);

    printf("reading the file info:...\n");
    while(fgets(inputStr, 255, (FILE*)fp) > 0) {
        printf(inputStr);
        Sleep(50);
    }

    printf("reading complete!\n");

    // fprintf(fp, "This is testing for fprintf...\n");
    // fputs("This is testing for fputs...\n", fp);

    printf("input you want to written message : ");
    gets(inputStr); // 输入:#clear,可以执行清空文件命令
    if (*inputStr == '#') {
        printf("->enter the cmd : ...\n");
        if (strcmp((inputStr + 1),"clear")==0) {
            printf("cmd:clear action ...");
            fclose(fp);
            fp = fopen(filename, "w+");
            printf("cmd clear action complete!\n");
        } else {
            printf("unhandle cmd : %s\n", (inputStr + 1));
        }
        printf("            
关注
打赏
1664331872
查看更多评论
0.1322s