为什么以下程序有效

发布于 2024-11-14 22:03:56 字数 202 浏览 1 评论 0原文

我编写了以下程序,

#include<stdio.h>
 main ()
{
        extern int i;
        printf("\n%d",i);
}
int i=30;

因为我在 main 之后初始化,所以我期待一条错误消息,但相反,该程序给了我输出。我想知道为什么它没有给我错误。

I wrote following program

#include<stdio.h>
 main ()
{
        extern int i;
        printf("\n%d",i);
}
int i=30;

I was expecting an error message as i is initialized after main but on the contrary the program gave me output.Why it did not gave me an error is what I want to know.

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

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

发布评论

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

评论(5

戒ㄋ 2024-11-21 22:03:56

extern 的全部目的是它表示“项目中的某处有一个名为 iint 类型变量,可以链接该变量稍后就假设它存在”。

您可以在完全独立的 .c 文件中定义 i,只要将 .o 文件链接在一起,它仍然可以工作。 这就是 extern 的作用

这就像声明一个函数并使用它一样,即使它是在完全独立的 .c 文件中定义的(或者实际上是稍后在同一个文件中定义的)。

阅读 C 书籍中有关 extern 的章节。

The entire purpose of extern is that it says "there is a variable of type int called i, somewhere in the project, that may be linked in later. just assume it exists".

You could define i in an entirely separate .c file and it'd still work as long as you linked the .o files together. That's what extern does.

It's just like how you can declare a function and use it, even if it's defined in a completely separate .c file (or, indeed, later on in the same one).

Read the chapter in your C book about extern.

溺孤伤于心 2024-11-21 22:03:56

因为代表i的符号仍然存在于程序空间中。通过将其声明为“extern”,您告诉编译器在遇到“i”之前不一定期望它的定义......换句话说,您明确告诉编译器相信该符号将在稍后链接。

从根本上讲,这与在完全独立的库中定义函数并在 main 中将其声明为 extern 没有任何不同。顺序并不重要,因为符号仍将被链接。

Because the symbol representing i is still present in the program space. By declaring it "extern", you're telling the compiler NOT to necessarily expect the definition of "i" before encountering it...in other words, you're explicitly telling the compiler to trust that the symbol will be linked in later.

This, fundamentally, not any different than having a function definition in a completely separate library and declaring it extern in your main. The order is unimportant, as the symbol will still be linked in.

驱逐舰岛风号 2024-11-21 22:03:56

extern 是在当前模块外部定义的东西。如果您的声明稍后出现,或者即使您的声明位于尚未遇到的其他文件中,您也可以使用 extern。

[节省您的精力-->下面的行来自 WIKIPEDIA]

当您定义变量时,您是在告诉编译器为该变量分配内存,并且还可能将其内容初始化为某个值。

当您声明变量时,您是在告诉编译器该变量是在其他地方定义的。

您只是告诉编译器存在具有该名称和类型的变量,但编译器不应为其分配内存,因为它是在其他地方完成的。

extern 关键字的意思是“声明而不定义”。换句话说,它是一种显式声明变量或在没有定义的情况下强制声明的方法。

了解详情:http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C#ixzz1OzrWVmAC

An extern is something that is defined externally to the current module. You could use extern in case your declaration comes later, or even when your declaration is in some other file, not yet encountered.

[SAVING YOU EFFORT --> lines below are FROM WIKIPEDIA]

When you define a variable, you are telling the compiler to allocate memory for that variable, and possibly also to initialize its contents to some value.

When you declare a variable, you are telling the compiler that the variable was defined elsewhere.

You are just telling the compiler that a variable by that name and type exists, but the compiler should not allocate memory for it since it is done somewhere else.

The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition.

Read more: http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C#ixzz1OzrWVmAC

百合的盛世恋 2024-11-21 22:03:56

如果您想更深入地了解如何从 main 访问 i 以及它何时初始化,您可以查看示例程序集输出。正如下面的评论中所指出的,它来自一个工具链(gcc/linux),但应该有助于提供一幅很好的图片。它表明 i 位于数据段中,并在执行 main 之前初始化。

    .file   "test.c"
    .section    .rodata
.LC0:
    .string "\n%d"
    .text
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    movl    i, %edx
    movl    $.LC0, %eax
    movl    %edx, 4(%esp)
    movl    %eax, (%esp)
    call    printf
    leave
    ret
    .size   main, .-main
.globl i
    .data
    .align 4
    .type   i, @object
    .size   i, 4
i:
    .long   30
    .ident  "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
    .section    .note.GNU-stack,"",@progbits

If you want a more in-depth look at how i is accessed from main and when it's initialized, you can look at sample assembly output. As noted in the comment below, it's from one toolchain (gcc/linux), but should help give a good picture. It shows that i is in the data segment, and initialized prior to executing main.

    .file   "test.c"
    .section    .rodata
.LC0:
    .string "\n%d"
    .text
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    movl    i, %edx
    movl    $.LC0, %eax
    movl    %edx, 4(%esp)
    movl    %eax, (%esp)
    call    printf
    leave
    ret
    .size   main, .-main
.globl i
    .data
    .align 4
    .type   i, @object
    .size   i, 4
i:
    .long   30
    .ident  "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
    .section    .note.GNU-stack,"",@progbits
·深蓝 2024-11-21 22:03:56

我期待一条错误消息,因为 i 在 main b 之后初始化

全局变量(因为所有具有静态存储持续时间的变量)在 main() 中的代码开始执行之前初始化,无论您在代码中的何处定义了它们。

I was expecting an error message as i is initialized after main b

Global variables(as all variables with static storage duration) are initiaized before code in main() starts executing regardless of where in the code you've defined them.

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