Ads 468x60px

##EasyReadMore##

26 9月, 2017

那些程序C語言可以編譯通過但C++無法編譯成功 Write a C program that won't compile in 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$ 

 [C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++ - 穆穆兔兔 - 博客园 - https://goo.gl/WSjfKU

0 意見:

張貼留言

 
Blogger Templates