一些值得尝试的代码

<aside> 👉

Welcome to my C programming notes!

This is a collection of interesting C code snippets and concepts I've gathered. Feel free to explore, learn, and experiment with these examples. This page is being continuously updated, so be sure to check back often for new content!

If you have any questions, suggestions, or want to discuss any of these topics further, please don't hesitate to make a comment or reach out to me. Your input is valuable and appreciated!

</aside>

响铃符,在外部终端运行时会发出声音🔔

#include<stdio.h>
int main(){
    printf("\\a");
    return 0;
}

float 和 double 并非准确值

#include<stdio.h>
int main(){
    for (float i = 0; i < 100; i+=1)
    {
        printf("%10.20f\\n", i/100);
    }
    return 0;
}

<aside> ⚠️

float 和 double 不能进行位运算和取模(或被取模)操作

</aside>

字符串末尾都是空字符 ‘\0’

#include<stdio.h>
int main(){
    char str[10]="world";
    char str2[10]="";
    for (int i = 0; i < 10; i++)
    {
        if(str[i]=='\\0'){
            printf("\\\\0 ");
        }
        else{
            printf("%c ",str[i]);
        }
    }
    printf("\\n");
    for (int i = 0; i < 10; i++)
    {
        if(str2[i]=='\\0'){
            printf("\\\\0 ");
        }
        else{
            printf("%c ",str2[i]);
        }
    }

    return 0;
}

输出一个数的二进制(是补码)

#include<stdio.h>
int main(){
    while(1){
        short a;
        scanf("%hd",&a);
        for (int i = 15; i >=0; i--)
        {
            printf("%d",a>>i&1);
            if(i==8)printf(" ");
        }
        printf("\\n");

    }
    return 0;

}

开一个长度为变量的数组

#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    int a[n];
    for (int i = 0; i < n; i++)
    {
        printf("%d ",a[i]);
    }
}

a[n]必须是局部变量,若在全局定义,会报错 variably modified 'a' at file scope

#include<stdio.h>
int n=10; //在c中,即使改为const int 也会报错,改为cpp文件后正常
int a[n];

int main(){
    for (int i = 0; i < n; i++)
    {
        printf("%d ",a[i]);
    }
}

局部变量是未经初始化的,全局变量被初始化为零。

#include<stdio.h>
int a[100];
int main(){
    for (int i = 0; i < 100; i++)
    {
        printf("%d ",a[i]);
    }
}

结果全为0

#include<stdio.h>
int main(){
    int a[100];
    for (int i = 0; i < 100; i++)
    {
        printf("%d ",a[i]);
    }
}

结果为乱七八糟的数