为什么从 main() 返回 NULL?
Why returning NULL from main()?
我有时会看到编码器在 C 和 C++ 程序中使用 NULL
作为 main()
的 return 值,例如:
#include <stdio.h>
int main()
{
printf("HelloWorld!");
return NULL;
}
当我用 gcc 编译这段代码时,我收到以下警告:
warning: return makes integer from pointer without a cast [-Wint-conversion]
这是合理的,因为宏 NULL
应扩展为 (void*) 0
并且 main 的 return 值应为 int
.
类型
当我编写一个简短的 C++ 程序时:
#include <iostream>
using namespace std;
int main()
{
cout << "HelloWorld!";
return NULL;
}
并用 g++ 编译它,我得到了一个等效的警告:
warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
但是为什么他们在抛出警告时使用 NULL
作为 main()
的 return 值?仅仅是糟糕的编码风格吗?
- 尽管有警告,但仍使用
NULL
而不是 0
作为 main()
的 return 值的原因是什么?
- 它是否由实现定义,是否合适?如果是,为什么任何实现都想要取回指针值?
which is reasonable
是的。
because the macro NULL
shall be expanded to (void*) 0
没有。在 C++ 中,宏 NULL
不得 扩展为 (void*) 0
[support.types.nullptr]。它可能只在 C 中这样做。
无论哪种方式,写这样的代码都会产生误导,因为 NULL
应该引用 空指针常量 ,无论它是如何实现的。用它代替 int
是逻辑错误。
- What is the reason to use
NULL
instead of 0 as return value of main()
despite the warning?
无知。没有 好的 这样做的理由。
- Is it implementation-defined if this is appropriate or not and if so why shall any implementation would want to get a pointer value back?
不,它永远不合适。编译器 是否允许 取决于实现。符合标准的 C++ 编译器可能会在没有警告的情况下允许它。
在?some/many/all? C++ 实现 NULL
是扩展为 0
.
的宏
这在展开时实际上给出 return 0
。这是一个有效的 return 值。
Is it just bad coding style?
是的。
When I compile this `code with gcc I get the warning of:
warning: return makes integer from pointer without a cast
这是因为您使用宽松的编译器选项进行编译。使用严格的 C 标准设置 -std=c11 -pedantic-errors
,您将在 NULL
扩展为空指针常量 (void*)0
的实现中得到预期的编译器错误。参见 。
在 NULL
扩展到 0
的实现中,代码严格来说是符合标准的,但风格非常糟糕,不可移植,最糟糕的是:完全是胡说八道。
And compile it with g++, I do get an equivalent warning:
warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
在 C++11 及更高版本中,不应使用 NULL
- 而是使用 nullptr
。 return 无论如何,来自 main() 的它都是不正确的。 NULL
在 C++ 中总是扩展为 0
所以严格来说它会起作用,但它是非常糟糕的风格,最糟糕的是:完全是胡说八道。
Is it just bad coding style?
不仅糟糕,而且毫无道理的无意义编码风格。写的程序员无能
Is it just bad coding style?
更糟。表明程序正常完成的正确方法是
#include <stdlib.h>
int main (void)
{
return EXIT_SUCCESS;
}
我有时会看到编码器在 C 和 C++ 程序中使用 NULL
作为 main()
的 return 值,例如:
#include <stdio.h>
int main()
{
printf("HelloWorld!");
return NULL;
}
当我用 gcc 编译这段代码时,我收到以下警告:
warning: return makes integer from pointer without a cast [-Wint-conversion]
这是合理的,因为宏 NULL
应扩展为 (void*) 0
并且 main 的 return 值应为 int
.
当我编写一个简短的 C++ 程序时:
#include <iostream>
using namespace std;
int main()
{
cout << "HelloWorld!";
return NULL;
}
并用 g++ 编译它,我得到了一个等效的警告:
warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
但是为什么他们在抛出警告时使用 NULL
作为 main()
的 return 值?仅仅是糟糕的编码风格吗?
- 尽管有警告,但仍使用
NULL
而不是0
作为main()
的 return 值的原因是什么? - 它是否由实现定义,是否合适?如果是,为什么任何实现都想要取回指针值?
which is reasonable
是的。
because the macro
NULL
shall be expanded to(void*) 0
没有。在 C++ 中,宏 NULL
不得 扩展为 (void*) 0
[support.types.nullptr]。它可能只在 C 中这样做。
无论哪种方式,写这样的代码都会产生误导,因为 NULL
应该引用 空指针常量 ,无论它是如何实现的。用它代替 int
是逻辑错误。
- What is the reason to use
NULL
instead of 0 as return value ofmain()
despite the warning?
无知。没有 好的 这样做的理由。
- Is it implementation-defined if this is appropriate or not and if so why shall any implementation would want to get a pointer value back?
不,它永远不合适。编译器 是否允许 取决于实现。符合标准的 C++ 编译器可能会在没有警告的情况下允许它。
在?some/many/all? C++ 实现 NULL
是扩展为 0
.
这在展开时实际上给出 return 0
。这是一个有效的 return 值。
Is it just bad coding style?
是的。
When I compile this `code with gcc I get the warning of:
warning: return makes integer from pointer without a cast
这是因为您使用宽松的编译器选项进行编译。使用严格的 C 标准设置 -std=c11 -pedantic-errors
,您将在 NULL
扩展为空指针常量 (void*)0
的实现中得到预期的编译器错误。参见
在 NULL
扩展到 0
的实现中,代码严格来说是符合标准的,但风格非常糟糕,不可移植,最糟糕的是:完全是胡说八道。
And compile it with g++, I do get an equivalent warning:
warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
在 C++11 及更高版本中,不应使用 NULL
- 而是使用 nullptr
。 return 无论如何,来自 main() 的它都是不正确的。 NULL
在 C++ 中总是扩展为 0
所以严格来说它会起作用,但它是非常糟糕的风格,最糟糕的是:完全是胡说八道。
Is it just bad coding style?
不仅糟糕,而且毫无道理的无意义编码风格。写的程序员无能
Is it just bad coding style?
更糟。表明程序正常完成的正确方法是
#include <stdlib.h>
int main (void)
{
return EXIT_SUCCESS;
}