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. 【学习笔记】【C语言】关键字

    1.关键字就是C语言提供的有特殊含义的符号,也叫做“保留字” *C语言一共提供了32个关键字,这些关键字都被C语言赋予了特殊含义 auto double int struct break else l ...

  2. 多文件上传artDialog+plupload

    一.效果展示 包括文件上传面板以及文件上传列表 二.介绍 长话短说,采用spring springMVC mybatis maven mysql,实现多文件上传功能,下载使用的是流的形式. 其中涉及的 ...

  3. java中的异常处理机制_函数覆盖时的异常特点

    /*注意:异常声明在函数上 异常在子父类覆盖时的体现1.子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者异常的子类2.如果父类方法抛出多个异常,那么子类在覆盖该方法 ...

  4. 杭电2034——人见人爱A-B

    #include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...

  5. 在ArcGIS中WGS84大地坐标和投影平面坐标的转换

    以WGS84转换为北京54坐标为例: 首先你要先知道转化的参数,鉴于我国曾使用不同的坐标基准(BJ54.State80.Correct54),各地的重力值又有很大差异,所以很难确定一套适合全国且精度较 ...

  6. UIPickerView基本用法

    #import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  7. Ubuntu下MySQL忘记root密码重置

    MySQL忘记root密码肿么办?-_-|||   这种情况虽然不是很常见,但是有时长时间没有登录系统,还真会忘记密码.这时候,如果您能以系统管理员权限登陆密码,那还是有救的.放大招,将其重置即可. ...

  8. Linux C 程序 Linux网络编程(21)

    Linux网络编程网络编程必备的理论基础网络模型,地址,端口,TCP/IP协议 TCP/IP协议是目前世界上使用最广泛的网络通信协议日常中的大部分应用使用该系列协议(浏览网页,收发电子邮件,QQ聊天等 ...

  9. ArcGIS API for JavaScript介绍

    ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...

  10. i18next-页面层语言国际化js框架介绍

    因为工作需要,最近研究了下网站语言国际化的问题,根据当前项目架构,寻求一种较好的解决方案.首先总结下项目中语言切换实现方式大概有以下几种: 1,一种语言一套页面,如:index_CN.html,ind ...