简体   繁体   English

当我编译int main时真正发生了什么; 在C.

[英]What really happen when I compile int main; in C

What really happen when I compile: 编译时真正发生了什么:

int main; 

Isn't it supposed to be main() and caused an error ? 它不应该是main()并导致错误吗?

I tried to compile the code in CodeBlocks and it compiled perfectly without error. 我试图在CodeBlocks中编译代码,它编译完美而没有错误。

It's not an error as a C source file doesn't need to have a main function and you can define main as whatever you want as long as you don't try to define it twice in the same scope. 这不是错误,因为C源文件不需要具有main函数,只要您不尝试在同一范围内定义两次,就可以将main定义为您想要的任何内容。 As this is your whole program, it's fine - but the program certainly won't run correctly as there is no main function to find. 因为这是你的整个程序,所以很好 - 但程序当然不能正常运行,因为没有main功能可以找到。

All it does is declare a global (external) variable called main, initialised to 0. The linker would normally issue an error if it's not there, but it may be satisfied with presence of the external variable (I expect it's assuming it's a pointer). 它所做的就是声明一个名为main的全局(外部)变量,初始化为0.链接器通常会发出错误,如果它不存在,但它可能会满足外部变量的存在(我希望它假设它是一个指针) 。

EDIT: I looked into this a little with the debugger, and sure enough, main has a value of 0, ie the variable is being used as a pointer without a cast. 编辑:我用调试器调查了一下这一点,果然, main的值为0,即该变量被用作没有强制转换的指针。 So the initialisation code tries to run a function located at address 0, resulting in a segfault on my platform. 因此初始化代码尝试运行位于地址0的函数,从而在我的平台上产生段错误。

I think you are getting confused here due to incorrect (or lack of understand) of the scope. 由于范围不正确(或缺乏理解),我认为你在这里感到困惑。

When you say here a declaration of int main; 当你在这里说int main;的声明int main; is being passed by the compiler, I am assuming you are declaring it within a function scope. 正在由编译器传递,我假设你在函数范围内声明它。 But, if you try declaring it in global scope, then the compiler will throw an redefinition error. 但是,如果您尝试在全局范围内声明它,则编译器将抛出重新定义错误。

So, as long as you don't have two identical identifiers within the same scope, compiler will be satisfied and will let you have your way. 因此,只要您在同一范围内没有两个相同的标识符,编译器就会满意并让您拥有自己的方式。

The below code will give redeinition error: 以下代码将给出重新定义错误:

int main;

int main()
{
  printf("In main\n");
}

The below code won't because the scope of main is restricted inside the function only and compiler considers the int main variable and result is printed out as 5. 下面的代码不会,因为main的范围仅限于函数内部,编译器认为int main变量,结果打印为5。

int main()
{ 
   int main = 5;
   printf("In main, value of main is %d\n", main);
}

The below code however will print the address of main 但是下面的代码将打印main的地址

int main()
{
       printf("In main, value of main is %d\n", main);
}

EDIT: After reading through the comments, I feel the key problem here is that you are not having a main function at all, which you should have for a "C" program to start working. 编辑:阅读完评论后,我觉得这里的关键问题是你根本没有主要功能,你应该为“C”程序开始工作。 If you don't have a main function, but just declare an int main; 如果你没有main函数,只是声明一个int main; variable, your code might still compile, but when you execute there will be confusion and chaos as main is an integer variable, whereas it is expected to be a function. 变量,你的代码可能仍然可以编译,但是当你执行时会出现混乱和混乱,因为main是一个整数变量,而它应该是一个函数。 But, as long as you keep your main(s) under control as per my above answer, you should do fine. 但是,只要你按照我的上述答案控制你的主人,你应该做得很好。

You can go this link, which explains the concept of compilation and execution of a C program 你可以去这个链接,它解释了编译和执行C程序的概念

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

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