c语言编译成功,[C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++...
下面的程序可以用gcc編譯,但g++無法編譯。
#include
intmain()
{
foo();//foo() is called before its declaration/definition
}intfoo()
{
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 thisscope
foo();//foo() is called before its declaration/definition
2) C++中將一個非const指針指向一個const變量是非法的,但在C中是可以的。
#include
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", *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
intmain()
{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
intmain()
{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
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
intmain()
{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
intmain()
{inti;
i=5;
i++;intj;return 0;
}
8) C++存在loop variable,C中不存在。
#include
intmain()
{for(int i=0; i<5; i++)
;return 0;
}
9) C++ main函數必須返回int,C可以返回void
#include
void main (int argc, char *argv[])
{
printf("bye");
}
編譯結果:
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$
總結
以上是生活随笔為你收集整理的c语言编译成功,[C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言扫描图片的坐标,tc 如何在指定坐
- 下一篇: s3c2440移植MQTT