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. php_2

    form表单提交: <body> <form action="php_request2.php" method="post"> 姓名: ...

  2. Mysql 格式化日期格式

    DATE_FORMAT(date, format) 根据格式串format 格式化日期或日期和时间值date,返回结果串. 可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值, ...

  3. 8款超绚丽的jQuery焦点图动画

    随着前端技术和浏览器技术的不断发展,人们开始对网页视觉效果的要求越来越高.我们经常会在页面中看到很多炫酷的图片焦点图播放控件,有些甚至是大屏的焦点图占用大片的页面空间,从而吸引用户的眼球.本文要分享的 ...

  4. AOJ 0121 Seven Puzzle

    7 パズル 7 パズルは 8 つの正方形のカードとこれらのカードがぴたりと収まる枠で構成されています.それぞれのカードには.互いに区別できるように 0, 1, 2, ..., 7 と番号がつけられてい ...

  5. Rhythmbox中文乱码解决办法

    今天在网络上找到了一个比较好的解决Rhythmbox中文乱码的问题的方法 进入你的音乐文件夹执行如下代码: mid3iconv -e GBK *.mp3 如果没有提示多试几次,有可能系统会提示: py ...

  6. 为什么ARM的frq中断的处理速度比较快

    FRQ向量位于异常向量表的最末端,不需要跳转就可以直接执行后面跟随的异常处理程序:FRQ模式中私有寄存器数量最多,在进行异常处理时不需要对这些寄存器进行压栈保存.

  7. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法   单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h" ...

  8. Winform ListView根据条件定位到指定行

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 本地安装gem install --local redis-stat-0.4.13.gem

    因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...

  10. git管理和自动部署项目

    当一个项目需要纳入到版本控制的时候,选择的工具还是比较多的,最常见的就是工具有CVS,SVN,GIT等.在平时的开发中视情况而定,从来就没有最好的版本控制工具,只有最适合的工具.在这里我习惯用git来 ...