[C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++
http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/
1) C++中在函数声明之前调用一个函数会引发错误,但是在C中有可能可以。 参考 http://www.cnblogs.com/diegodu/p/4580292.html
下面的程序可以用gcc编译,但g++无法编译。
#include<stdio.h> int main() { foo(); // foo() is called before its declaration/definition } int foo() { printf("Hello"); return 0; }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test3.c diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test3.c test3.c: In function 'int main()': test3.c:4:9: error: 'foo' was not declared in this scope foo(); // foo() is called before its declaration/definition
2) C++中将一个非const指针指向一个const变量是非法的,但在C中是可以的。
#include <stdio.h> int main(void) { int const 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 const printf("*ptr: %d\n", *ptr); return 0; }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test4.c test4.c: In function 'main': test4.c:10:16: warning: initialization discards 'const' qualifier from pointer target type [enabled by default] int *ptr = &j; // A normal pointer points to const ^ diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test4.c test4.c: In function 'int main()': test4.c:10:17: error: invalid conversion from 'const int*' to 'int*' [-fpermissive] int *ptr = &j; // A normal pointer points to const ^
3) C中可以将void* 赋值给其他的指针,如int*, char*等,但是在C++中则需要显示的类型转换。
#include <stdio.h> int main() { void *vptr; int *iptr = vptr; //In C++, it must be replaced with int *iptr=(int *)vptr; return 0; }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test5.c diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test5.c test5.c: In function 'int main()': test5.c:5:17: error: invalid conversion from 'void*' to 'int*' [-fpermissive] int *iptr = vptr; //In C++, it must be replaced with int *iptr=(int *)vptr;
这也就意味着在C++中我们调用malloc时需要显示的转换类型,“int *p = (void *)malloc(sizeof(int)),而在C中不需要。
4) C++中const变量声明的同时必须赋初值,但C中不需要赋初值。
#include <stdio.h> int main() { const int a; // LINE 4 return 0; }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test6.c diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test6.c test6.c: In function 'int main()': test6.c:4:15: error: uninitialized const 'a' [-fpermissive] const int a; // LINE 4 ^
5) C++中的关键字在C中不可用,如new,delete,explicit等。
#include <stdio.h> int main(void) { int new = 5; // new is a keyword in C++, but not in C printf("%d", new); }
6) C++有着更加严格的类型检查。C++无法通过编译,有error,C中仅仅报warning
#include <stdio.h> int main() { char *c = 333; printf("c = %u", c); return 0; }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test7.c test7.c: In function 'main': test7.c:4:15: warning: initialization makes pointer from integer without a cast [enabled by default] char *c = 333; ^ test7.c:5:5: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'char *' [-Wformat=] printf("c = %u", c); ^ diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test7.c test7.c: In function 'int main()': test7.c:4:15: error: invalid conversion from 'int' to 'char*' [-fpermissive] char *c = 333; ^ test7.c:5:23: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'char*' [-Wformat=] printf("c = %u", c); ^
7) C++变量可以在任意位置定义,但是C中必须是一个语句块开始的地方。(gcc 中可以编译。。)
#include <stdio.h> int main() { int i; i=5; i++; int j; return 0; }
8) C++存在loop variable,C中不存在。
#include <stdio.h> int main() { for(int i=0; i<5; i++) ; return 0; }
9) C++ main函数必须返回int,C可以返回void
#include <stdio.h> void main (int argc, char *argv[]) { printf("bye\n"); }
编译结果:
diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test9.c diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test9.c test9.c:2:34: error: '::main' must return 'int' void main (int argc, char *argv[]) ^ diego@ubuntu:~/myProg/geeks4geeks/cpp$