📜  _C中的Noreturn函数说明符

📅  最后修改于: 2021-05-25 21:56:08             🧑  作者: Mango

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

// C program to show how _Noreturn type 
// function behave if it has return statement.
#include 
#include 
  
// 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 
#include 
  
// 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的C基础课程》。