编写一个不会在C ++中编译的C程序
在这里,我们将编写一些不会在c++中编译的c程序。尽管c++被认为是具有所有功能并与c代码兼容的c的继承者,但是有些程序在使用c++编译器编译时不会编译或出现编译错误。
一些不会在c++中编译的C程序的列表是-
在声明之前调用函数-在c++中,在声明之前调用函数会产生编译错误。但这在c中工作正常。
示例
#include <stdio.h>
int main(){
printHello();
return 0;
}
void printHello(){
printf("TutorialsPoint");
}输出结果
TutorialsPoint
使用类型转换的指针-如果我们在c中将指针声明为void,然后使用该指针指向其他数据变量。这可以由编译器本身在c中完成,但是在c++中,这些指针需要进行类型转换。
示例
#include <stdio.h>
int main(){
void *ptr;
int *ptr2 = ptr;
return 0;
}在不初始化的情况下声明常量值-在c中,您可以声明常量值而无需为其提供任何值,但是在c++中执行此操作会返回错误。
示例
#include <stdio.h>
int main(){
const int x;
printf("%d", x);
return 0;
}输出结果
0
在c++中,不允许将普通指针与const变量一起使用-而c允许将const变量与普通指针一起使用。
示例
#include <stdio.h>
int main(void){
int const x = 3424;
int *cptr = &x;
printf("value of pointer : %d\n", *cptr);
return 0;
}输出结果
Value of pointer: 3424
使用特定的关键字作为变量名-在c编程语言中,使用某些关键字作为变量名是有效的,即可以在c中编译,但不能在c++中编译。
示例
#include <stdio.h>
int main(void){
int class = 5;
printf("%d", class);
}输出结果
5
这些是c++中包含的关键字,另外一些是new,delete,explicit等。