C语言中的Noreturn函数说明符详细介绍

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

// 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...\n" ); view(); printf ( "NOT over till now\n" ); 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...\n" ); show(); printf ( "NOT over till now\n" ); return 0; }

输出如下:
Ready to begin...BYE BYE

参考:http://en.cppreference.com/w/c/language/_Noreturn
【C语言中的Noreturn函数说明符详细介绍】如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

    推荐阅读