#include<stdio.h>int main()
{
foo(); // foo() is called before its declaration/definition
}
int foo()
{
printf("Hello");
return0;
}
在C++中不允许普通指针指向常量,但是C中允许
#include <stdio.h>int main(void)
{
intconst j = 20;
/* The below assignment is invalid in C++, results in error
In C, the compiler *may* throw a warning, but casting is
implicitly allowed */int *ptr = &j; // A normal pointer points to constprintf("*ptr: %d\n", *ptr);
return0;
}
在C中void指针可以直接赋值给其他类型的指针,但是在C++中要做显示类型转换
#include <stdio.h>int main()
{
void *vptr;
int*iptr = vptr; //In C++, it must be replaced with int*iptr=(int*)vptr;
return0;
}
在C++程序中const常量必须初始化,否则编译错误,但C中允许
#include <stdio.h>int main()
{
constint a; // LINE 4return0;
}
很不幸的是,在C中我们可以使用C++的关键字new等作为变量名
#include <stdio.h>
int main(void)
{
int new = 5; //newis a keyword in C++, but notin C
printf("%d", new);
}
虽然C++一直保持着与C兼容,但是依旧有些不同的地方!需要注意纯C程序的问题!在C++当中,函数未声明先使用会出现编译错误,但是C编译器或许会通过#include&lt;stdio.h&gt;int main(){ foo(); // foo() is called before its declaration/definition} int foo(){ ...