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 ;
}

编译结果:

diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test3.c
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test3.c
test3.c: In function 'int main()':
test3.c::: 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 = ; /* 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 ;
}

编译结果:

diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test4.c
test4.c: In function 'main':
test4.c::: 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::: 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 ;
}

编译结果:

diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test5.c
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test5.c
test5.c: In function 'int main()':
test5.c::: 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 ;
}

编译结果:

diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test6.c
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test6.c
test6.c: In function 'int main()':
test6.c::: error: uninitialized const 'a' [-fpermissive]
const int a; // LINE 4
^

5) C++中的关键字在C中不可用,如new,delete,explicit等。

#include <stdio.h>
int main(void)
{
int new = ; // 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 = ;
printf("c = %u", c);
return ;
}

编译结果:

diego@ubuntu:~/myProg/geeks4geeks/cpp$ gcc test7.c
test7.c: In function 'main':
test7.c::: warning: initialization makes pointer from integer without a cast [enabled by default]
char *c = ;
^
test7.c::: warning: format '%u' expects argument of type 'unsigned int', but argument has type 'char *' [-Wformat=]
printf("c = %u", c);
^
diego@ubuntu:~/myProg/geeks4geeks/cpp$ g++ test7.c
test7.c: In function 'int main()':
test7.c::: error: invalid conversion from 'int' to 'char*' [-fpermissive]
char *c = ;
^
test7.c::: warning: format '%u' expects argument of type 'unsigned int', but argument has type 'char*' [-Wformat=]
printf("c = %u", c);
^

7) C++变量可以在任意位置定义,但是C中必须是一个语句块开始的地方。(gcc 中可以编译。。)

#include <stdio.h>
int main()
{
int i;
i=;
i++;
int j;
return ;
}

8) C++存在loop variable,C中不存在。

#include <stdio.h>
int main()
{
for(int i=; i<; i++)
; return ;
}

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::: 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++的更多相关文章

  1. [C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

    http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是 ...

  2. [C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C

    http://www.geeksforgeeks.org/g-fact-95/ 1 在C语言中,如果函数在声明之前被调用,那么编译器假设函数的返回值的类型为INT型, 所以下面的code将无法通过编译 ...

  3. [C/CPP系列知识] Type difference of character literals 和 bool in C and C++

    C/C+中的每一个常亮(every literal)都是有类型的,例如10 就是int型的,因此siziof(10)和sizeof(int)是相同的,但是字符型常亮(‘a’)在C和C++中有不同的变量 ...

  4. Golang 入门系列(三)Go语言基础知识汇总

    前面已经了 Go 环境的配置和初学Go时,容易遇到的坑,大家可以请查看前面的文章 https://www.cnblogs.com/zhangweizhong/category/1275863.html ...

  5. C语言的预编译,程序员必须懂的知识!【预编译指令】【预编译过程】

    由“源代码”到“可执行文件”的过程包括四个步骤:预编译.编译.汇编.链接.所以,首先就应该清楚的首要问题就是:预编译只是对程序的文本起作用,换句话说就是,预编译阶段仅仅对源代码的单词进行变换,而不是对 ...

  6. 《玩转D语言系列》二、D语言现状、基本规定和相关资源介绍

    这算是本系列文章的一个序吧,主要是为以后的学习做铺垫,文本分为三个部分,第一部分是对于网上一些比较旧的资料的问题的一些更正,当然我也不可能看过所有的资料,难免会有遗漏.第二部分是D语言最基本的规定,第 ...

  7. 【转】24Cxx 系列EEPROM通用程序及应用

    关于I2C 学习的时候介绍得最多的就是24C02 这里存储EEPROM了,但学的时候基本只是讲讲简单的I2C 的总线数据传输而已,即使先gooogle上搜索也绝大部分这这样的文章,很少有说到如何在实际 ...

  8. 用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署

    用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署 这是微软官方SignalR 2.0教程Getting Started with En ...

  9. 微信小程序开发语言的选择

    微信使用的开发语言和文件很「特殊」. 小程序所使用的程序文件类型大致分为以下几种: ①WXML(WeiXin Mark Language,微信标记语言) ②WXSS(WeiXin Style Shee ...

随机推荐

  1. dropdownlist值改变时调用js

    DropDownList的OnSelectedIndexChanged方法是服务器端方法如要用需要设置AutoPostBack选项为true,并且在服务器后台写方法 要调用js方法需要onchange ...

  2. 分享9款很有创意的HTML5动画

    1.HTML5 SVG Loading 动画加载特效 这是一款基于HTML5/CSS3和SVG的Loading加载动画特效,一共有4种不同的动画效果.每一组Loading动画都非常可爱,他们都非常欢快 ...

  3. Android基本知识

         Android是Google公司于2007年发布的基于Linux内核的手机操作系统.应用层主要以java为编程语言,应用层分为两层,函数层(Library) 和虚拟机(Virtual).中间 ...

  4. AnyCAD脚本模型

    采用AnyCAD建立的模型, 基于AnyCAD.Net SDK在C#中展示模型:

  5. Linux 线程 条件变量

    一:条件变量 直接上最基本的两个函数,先抓主要矛盾: //等待条件 int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex ...

  6. zedboard 驱动理解

    1 驱动程序的编写   驱动是LINUX开发的必经之路,应用层对底层的调用经过了库与内核,内核下面才是驱动层,当你在应用程序执行对底层的控制时,驱动程序为你的控制提供了接口,或者说是策略. #incl ...

  7. JS重写alert,保证弹窗错误的友好性

    // ------------------------------------------------------------- // 重写alert,保证弹窗错误的友好性 var j_oldAler ...

  8. .NET SOCKET通信编程

    1 using System; 2 using System.Net; 3 using System.Net.Sockets; 4 using System.Text; 5 6 public clas ...

  9. ubuntu 停在开机界面

    今天有解决了一个问题.我在win7虚拟机上装的64位的Ubuntu 12.04.忘了怎么个情况了,反正就是系统进不去了,停在了开机界面,5个点的那个. 解决方法如下: 开机的时候按住shift键,进入 ...

  10. CDH JPS 出现没有名字的进程

    jps 时出现没有名字的进程 或者process information unavailable 把服务关掉,执行一下 rm -rf /tmp/hsperfdata_* 再重启就好了.