c语言编译成功,[C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++...

本文探讨了C与C++在编译、指针使用、类型转换、常量声明、关键字、类型检查和变量定义等方面的差异。C++引入了更严格的类型检查和对象导向特性,如类和模板,而C语言则更注重底层内存管理和指针操作。这些差异在实际编程中可能导致不同的错误处理和警告。
摘要由CSDN通过智能技术生成

下面的程序可以用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$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值