编写一个不能在C中编译的C程

北京皮炎哪家医院好 http://m.39.net/baidianfeng/a_9057338.html

尽管C++语言是C语言的超集,也就是C++会兼容C,但是在使用C++编译器编译的时候,可可能能会出现许多C程序会产生编译器错误。以下的C程序情况就无法在C++编译器中编译:

在声明之前调用函数

使用带有const变量的普通指针

使用类型转换的指针

声明常量值而不初始化

使用特定关键字作为变量名

严格的类型检查

main()的返回类型

下面将详细讨论这些要点:

1.在声明之前调用函数:在C++中,在声明之前调用函数是编译器错误。但在C中,它可以编译。(请参阅在C中声明函数之前调用函数会发生什么?)

//CProgramtodemonstratecalling//afunctionbeforedeclaration#includestdio.h//Mainstartsintmain(){//fun()iscalledbeforeits//declaration/definitionfun();}//FunctionDeclarationintfun(){printf("Hello");return0;}

2.使用带有const变量的普通指针:在C++中,当使用普通指针指向const变量时会产生编译器错误,但在C中是允许的。(必读-C中的ConstQualifier)

//CProgramtodemonstrateusinga//normalpointerwithconstvariable#includestdio.h//Mainstartsintmain(){//Anormalpointerpointstoconstintconstj=20;int*ptr=j;//ThebelowassignmentisinvalidinC++,//resultsinerror.InC,the


转载请注明:http://www.aierlanlan.com/rzgz/706.html