_C语言中的Noreturn函数说明符

删除“noreturn”关键字后,C编程语言的C11标准(称为最终草案)引入了一个新的“noreturn”函数说明符,该说明符指定函数不会返回到调用它的函数。如果程序员试图从该函数返回任何声明为_Noreturn类型的值,那么编译器会自动生成编译时错误。

null

// C program to show how _Noreturn type
// function behave if it has return statement.
#include <stdio.h>
#include <stdlib.h>
// With return value
_Noreturn void view()
{
return 10;
}
int main( void )
{
printf ( "Ready to begin..." );
view();
printf ( "NOT over till now" );
return 0;
}


输出:

Ready to begin...
After that abnormal termination of program.
compiler error:[Warning] function declared 'noreturn' has a 'return' statement

// C program to illustrate the working
// of _Noreturn type function.
#include <stdio.h>
#include <stdlib.h>
// Nothing to return
_Noreturn void show()
{
printf ( "BYE BYE" );
}
int main( void )
{
printf ( "Ready to begin..." );
show();
printf ( "NOT over till now" );
return 0;
}


输出:

Ready to begin...
BYE BYE

参考: http://en.cppreference.com/w/c/language/_Noreturn

本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享