QuickReference/source/_posts/language/C.md

82 lines
1.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: C lang
tags: C C++
abbrlink: 12462
date: 2025-01-15 20:41:26
---
### c lang在windows下的开发VS code
[WinLibs - GCC+MinGW-w64 compiler for Windows](https://winlibs.com/#download-release)下载你需要的版本
解压到`D:\ProgramModule`,并将 `bin\`加入环境变量`PATH`
打开新的`Terminal`输入`gcc -v`,查看`gcc`是否安装成功
在`VS code` 的插件管理下载`Code Runner`、`C\C++`这两个插件
在`*.c`源文件的内容区,右键点击`Run Code` ,即可运行成功
![](/img/language/c-env-conf.png)
### 数据类型
- 整数类型
```c
    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);
```
- 小数类型
```c
float f = 3.14F;
printf("f: %.3f\n",f);
double g = 5.65;
printf("g: %.2lf\n",g);
```
- 字符类型
```c
char h = 'x';
printf("x: %c\n",x);
```
### 类型转换
- 隐式转换
- 强制转换
```c
int b = 23;
short c = (short) b;
```
### 数组
```c
#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;
}
```
### 指针
```c
// 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);
```