简体   繁体   English

C中是否需要变量声明?

[英]Is variable declaration necessary in C?

See the comments to see what is being referred as declaration. 请参阅注释以查看被称为声明的内容。 If the whole variable declaration part was missing, what would be the problem? 如果缺少整个变量声明部分,那会是什么问题?

Appears that variable definition and initialization either simultaneously or separately like in the example would suffice. 似乎在示例中同时或单独地进行变量定义和初始化就足够了。

#include <stdio.h>

// Variable declaration:
extern int a, b;
extern int c;
extern float f;

int main () {

   /* variable definition: */
   int a, b;
   int c;
   float f;

   /* actual initialization */
   a = 10;
   b = 20;

   c = a + b;
   printf("value of c : %d \n", c);

   f = 70.0/3.0;
   printf("value of f : %f \n", f);

   return 0;
}

If the declaration was missing then it would create no problem in main function since the locally defined variables ie a,b,c,f will be used in the functionality of main till its scope ends. 如果声明丢失,那么它将在main函数中没有问题,因为本地定义的变量,即a,b,c,f将用于main的功能,直到其范围结束。

The declaration merely tells that the definition lies elsewhere (in some other .c file) or the definition lies after the function main in the same .c file. 声明只是告诉定义位于别处(在其他.c文件中)或定义位于同一.c文件中的函数main之后。

There will be no problem here if the mentioned declaration is missing. 如果缺少上述声明,这里就没有问题了。

// Variable declaration:
extern int a, b;
extern int c;
extern float f;

This tells the compiler that these variables are defined somewhere else(in another file). 这告诉编译器这些变量是在其他地方定义的(在另一个文件中)。

/* variable definition: */
int a, b;
int c;
float f;

This is where you define variables but they are not the same as the external variables you declared since they are in the inner scope of the main function. 这是您定义变量的地方,但它们与您声明的外部变量不同,因为它们位于main函数的内部范围内。

The scope is the place where variables live. 范围是变量存在的地方。 extern keyword notes that the scope is global. extern关键字指出范围是全局的。 You can define variables with the same name in an inner scope and access only them as you did in the main function but it's not a good practice. 您可以在内部作用域中定义具有相同名称的变量,并且只能像在main函数中那样访问它们,但这不是一个好习惯。

void foo()
{
    int a = 5;
    printf("%d\n", a); // 5

    // Creating an inner scope
    {
        int a = 20;
        printf("%d\n", a); // 20
    }
    printf("%d\n", a); // 5
}

The correct way to use the extern keyword with variables is like this. extern关键字与变量一起使用的正确方法是这样的。

//h1.h
extern int global_var;  // Declaration of the variable

//c1.c
#include h1.h
int global_var = 0; // Definition of the global var. Memory is allocated here.

//main.c
#include h1.h
int main()
{
    printf("global var value is %d\n", global_var); // use of the var defined and 
                                                    // initialized in c1.c
    return 0;
}

This program will print 0 since the variable is defined and initialized in c1.c. 由于变量是在c1.c中定义和初始化的,因此该程序将打印0

Extern extends the visibility of the C variables and C functions. Extern扩展了C变量和C函数的可见性。 so that lets the compiler know that there is another place that those vars are declared and memory was allocated for them elsewhere. 因此,让编译器知道有另一个地方声明这些变量并在其他地方为它们分配了内存。 for example in another c file. 例如在另一个c文件中。 if you compile ac file containing a global var for example: int c = 5; 如果编译包含全局变量的ac文件,例如:int c = 5; and you create a function on you c file that uses this c var, for example: 并在您使用此c var的c文件上创建一个函数,例如:

int someFunc(void){

return c;}

if you run someFunc in your main and print its return value, you will get 5. but you must compile both c files together. 如果你在main中运行someFunc并打印它的返回值,你将获得5.但是你必须将两个c文件一起编译。 in your program, you only use the locally allocated var declared in your main function. 在您的程序中,您只使用在main函数中声明的本地分配的var。

When it comes to simple variables, there is really no difference between the declaration and definition. 说到简单变量,声明和定义之间确实没有区别。 There is a difference when it comes to structs and functions. 结构和功能有所不同。 Here is an example: 这是一个例子:

// Declarations
struct myStruct;
int foo();

int main() 
{
   ...
}

// Definitions
struct myStruct {
    int a, b;
};

int foo() {
    return 42;
}

In your case, you are hiding the previous declarations so that they are not accessible before the end of the scope. 在您的情况下,您隐藏了以前的声明,以便在作用域结束之前无法访问它们。 This is commonly called shadowing . 这通常称为阴影 It's basically the same thing as this: 它与此基本相同:

int main()
{
    int i=0;
    printf("i: %d\n", i);
    {
        int i=42; // Now the previous i is inaccessible within this scope
        printf("i: %d\n", i);
    }
    // And now it is accessible again
    printf("i: %d\n", i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM