C中变量声明和定义的区别。什么时候分配内存?

Difference between variable declaration and definition in C. When does the memory gets allocated?

所以我一直在努力自学C。所以只有几个视频和文章以及我身边的一本书。虽然这听起来像是一个简单的概念(我敢肯定是这样),但我认为这个概念对我来说很清楚。

当一个变量被声明定义[=时,你能引用一个例子吗? 34=]?(一起和分开)

就像我在一些文章或论坛上看到的那样

int x; (声明了 x)

某处写着

int x; ( x 已定义)。

内存什么时候分配给变量? 又在某处据说必须首先定义变量才能分配内存,而在某处据说它是在声明变量时分配的?

我希望这能让您大致了解变量何时声明、分配、初始化、释放和消失(销毁)。

/* global variable A declared and memory for int is allocated */
/* memory will only be unallocated at global program exit */
/* global variable A initialized/defined with value 10 */
int A = 10;

void test(int D){
    /* function test is called by main with argument test(5) */
    /* local variable D is declared, memory for int is allocated */
    /* and initialized with value 5 */
    
    /* add local variable D to global variable A */
    A = A + D;
    
    /* after this point, local variable D will be deallocated */
    /* and will vanish */
}

int main(int argc, char *argv[]){
    /* local variable B declared, memory for int allocated */
    /* and initialized with value 20 */
    int B = 20;
    
    {
        /* local scoped variable C declared, memory for int allocated */
        /* and initialized with value 30 */
        int C = 30;
        /* after this point, local scoped variable C will be */
        /* deallocated and will vanish */
    }
    
    /* 5 is a local scoped const of size int, memory will be allocated */
    test(5);
    /* after this point, local scoped const 5 will be deallocated */
    /* and will vanish */
    
    
    /* after this point, local variable B will be deallocated */
    /* and will vanish */
    return A;
}

Like in some articles or forums I read they say Int x; ( x is declared) Somewhere it's written Int x; ( x is defined).

实际上 int x; 同时声明和定义它,所以两者都有效但不完整。

声明在不创建变量的情况下向编译器显示变量的类型。

extern int x; // <- this is declaration

定义创建对象,如果对象在其定义之前声明必须匹配声明。

extern int x;
int x;

有效但是

extern int x;
double x; 

不是。