具有值的变量的定义和声明

发布于 2024-10-19 08:49:16 字数 385 浏览 2 评论 0原文

刚开始使用 K & R 在第二章中,有这样一行:

声明列出了要声明的变量 使用过并说明它们的类型以及 也许它们的初始值是什么

所以:

int x = 42 是一个定义

int x 是一个声明,但也是一个定义,因为每个定义都是一个声明 >。

但是当我们分配一个像 K & 这样的初始值时R 说,这不是使声明成为定义吗?

Just started with K & R and on the 2nd chapter, there is the line:

Declarations list the variables to be
used and state what type they have and
perhaps what their initial values are.

So:

int x = 42 is a definition.

and int x is a declaration but also a definition since every definition is a declaration.

But when we assign an intial value like K & R say, doesn't that make the declaration a definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-10-26 08:49:17

基本上,您可以说声明只是告诉编译器某处存在具有该名称和类型的变量。它确实会生成任何代码,并且在 C 中,这必须使用变量上的 extern 关键字来完成。函数原型(没有实现)也只是声明,不需要 extern 关键字,但您仍然可以提供它。
定义生成代码,例如在堆栈或堆上为变量分配内存,或为方法分配主体。

从这个意义上说,你的两个陈述都是定义,任何定义也是一种声明。

我认为这可能是 K&R 的一个错误......

Basically you can say that a declaration simply tells the compiler that there is somewhere a variable with that name and type. It does produce any code, and in C, this has to be done with the extern keyword on variables. Function prototypes (without implementation) are also mere declarations, and don't need the extern keyword, but you can still provide it.
A definition produces code, e.g. allocates memory on the stack or heap for a variable, or the body for methods.

In that sense, both of your statments are definitions, and any definition is also a delcaration.

I think that might by a mistake by K&R...

蓝眼泪 2024-10-26 08:49:16

您会混淆两件事:

  1. 声明声明(声明)对象的*类型、名称和范围是什么
  2. 定义定义对象的内容是什么

* 对象如:变量,函数等,而不是 OOP 对象。

因此,定义通常也是声明,因为当您不声明对象的类型时,您无法定义对象中的内容。最容易记住的是:“每个定义都是声明,但并非每个声明都是定义”

对于变量

只有一种方法可以在不定义变量的情况下进行声明:

extern typeX variable_name

这告诉编译器有一个变量名为variable_name,类型为typeX,但不知道从哪里获取它。
声明变量的所有其他方式也是一种定义,因为它告诉编译器为其保留空间,并且可能给它一个初始值。

结构和函数的区别更加明显:

对于结构

声明:

struct some_struct{
    int a;
    int b;
}

这向编译器声明 some_struct,其中 a 和 b 都是 int 类型的结构变量。

仅当您定义它们时,才会保留空间并且您可以使用它们:

void foo(){
    struct some_struct s;
    s.a = 1; // For this to work s needs to be defined
}

对于函数

区别在于更清晰的

声明:

// This tells the compiler that there is a function called "foo" that returns void and takes void arguments
void foo();

定义可以类似于上面的定义(在结构部分)

You confuse two things:

  1. A declaration states (declares) what an object's* type, name and scope is
  2. A definition defines what a object's content is

* object as in: variable, function etc., not an OOP object.

A definition is hence very often also a declaration, since you cannot define what is in an object when you do not state what the type of the object is. Easiest to remember is just: "Every definition is a declaration, but not every declaration is a definition"

For variables

There is only 1 way to declare without defining the variable:

extern typeX variable_name

This tells the compiler that there is a variable called variable_name with type typeX, but not where to get it.
Every other way to declare a variable is also a definition, since it tells the compiler to reserve space for it and perhaps give it an initial value.

The difference is much clearer in structs and functions:

For Structs

A declaration:

struct some_struct{
    int a;
    int b;
}

This declares some_struct to the compiler with a and b as struct variables both with type int.

Only when you define them space is reserved and you can use them:

void foo(){
    struct some_struct s;
    s.a = 1; // For this to work s needs to be defined
}

For functions:

The difference is much more clear

declaration:

// This tells the compiler that there is a function called "foo" that returns void and takes void arguments
void foo();

A definition could be like the one above (in the struct part)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文