这是code下的任何C标准是否有效? [英] Is this code valid under any C standard?

查看:127
本文介绍了这是code下的任何C标准是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否code遵循C类标准(例如C89,C99,C10x)?

 无效
主(INT A,INT B,INT C,INT D,字符* MSG){
    如果(D == 1){
        的printf(%S \\ n,味精);
    }其他{
        主(1,2,3,1,和放大器;你好#1);
    }
}

如果不是,为什么?


解决方案

还有一个错误:&安培;你好#1没有键入字符* ,所以你不应该传递给需要使用该类型的函数。

除此之外,该计划是由标准允​​许为实现特定的扩展名,但编译器可以自由地拒绝它。


  

称为在程序启动时的功能被命名为。实施没有声明
  原型此功能。它应 INT 的返回类型,并没有被定义
  参数:


  INT主要(无效){/ * ... * /}


  

或两个参数(这里称为 ARGC 的argv ,虽然任何名称可能是
  使用,因为它们是局部的,其中声明它们的功能):


  INT主(INT ARGC,CHAR *的argv []){/ * ... * /}


  

或同等学历; 或一些其他实现定义的方式


(2011年标准,最新款5.1.2.2.1草案,加上强调)。

有上递归调用 C标准中没有禁止。这是C ++,它不按规矩一个区别。

Does this code follow C standards (e.g. C89, C99, C10x)?

void 
main(int a,int b, int c, int d,char *msg){
    if(d==1){
        printf("%s\n",msg);
    }else{
        main(1,2,3,1,&"Hello Stackoverflow");
    }
}

If not, why?

解决方案

There's one error: &"Hello Stackoverflow" does not have type char*, so you shouldn't pass that to a function expecting that type.

Apart from that, this program is allowed by the Standard as an implementation-specific extension, but a compiler has the freedom to decline it.

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

(2011 Standard, latest draft section 5.1.2.2.1, emphasis added.)

There is no ban on recursive calls to main in the C Standard. This is a difference with C++, which does outlaw that.

这篇关于这是code下的任何C标准是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆