c lang在windows下的开发(VS code)

WinLibs - GCC+MinGW-w64 compiler for Windows下载你需要的版本
解压到D:\ProgramModule,并将 bin\加入环境变量PATH
打开新的Terminal输入gcc -v,查看gcc是否安装成功
VS code 的插件管理下载Code RunnerC\C++这两个插件
*.c源文件的内容区,右键点击Run Code ,即可运行成功

数据类型

  • 整数类型
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        short a = 12;
        int b = 100;
        long c = 1000L;
        long long d = 1000000LL;
        unsigned int e = 10;
        printf("a: %hd\n",a);
        printf("b: %d\n",b);
        printf("c: %ld\n",c);
        printf("d: %lld\n",d);
        printf("e: %u\n",e);
        printf("f: %.3f\n",f);
  • 小数类型
    1
    2
    3
    4
    float f = 3.14F;
    printf("f: %.3f\n",f);
    double g = 5.65;
    printf("g: %.2lf\n",g);
  • 字符类型
    1
    2
    char h = 'x';
    printf("x: %c\n",x);

类型转换

  • 隐式转换
  • 强制转换
    1
    2
    int b = 23;
    short c = (short) b;

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main(){
    int arr [10] = {2,3,4,5,6,7,8,9,10,11};
    arr[0] = 1525;
    *(arr+1) = 25;
    int len = sizeof(arr)/sizeof(arr[0]);
    void printArr(int arr[], int len){
        for (int i = 0; i < len;i++){
            printf("%d\t",arr[i]);
        }
    }
    printArr(arr,len);
    return 0;
}

指针

1
2
3
4
5
6
7
8
9
10
11
// swap the value of a and b
    void swap(int* x, int* y){
        int temp = *x;
        *x = *y;
        *y = temp;

    }
    int a = 5;
    int b = 10;
    swap(&a, &b);
    printf("a = %d b = %d\n", a, b);