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

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

编译结果:

[email protected]:~/myProg/geeks4geeks/cpp$ gcc test3.c
[email protected]:~/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;
}

编译结果:

[email protected]:~/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
                ^
[email protected]:~/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;
}

编译结果:

[email protected]:~/myProg/geeks4geeks/cpp$ gcc test5.c
[email protected]:~/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;
}

编译结果:

[email protected]:~/myProg/geeks4geeks/cpp$ gcc test6.c
[email protected]:~/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;
}

编译结果:

[email protected]:~/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);
     ^
[email protected]:~/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");
}

编译结果:

[email protected]:~/myProg/geeks4geeks/cpp$ gcc test9.c
[email protected]:~/myProg/geeks4geeks/cpp$ g++ test9.c
test9.c:2:34: error: ‘::main‘ must return ‘int‘
 void main (int argc, char *argv[])
                                  ^
[email protected]:~/myProg/geeks4geeks/cpp$ 

 

 

 

时间: 2024-08-05 21:56:31

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

[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++编译器是如何区分不同的函数的呢?----是通过在函数名是加些信息来区不同的函数,即所谓的Name Mangling.C++标准并没有对name mangling技术,各个编译器可以添加不同的信息. 考虑下面的函数 int f (void) { return 1; } int f (int) { return 0; } void g (voi

[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++中有不同的变量类型. 在C中,‘a’被认为是int形,在C++中,‘a’被认为是char型. int main() { printf("sizeof('V') = %d sizeof(char) = %d", sizeof('V'), sizeof(char)); return 0; } 结果: C r

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

由“源代码”到“可执行文件”的过程包括四个步骤:预编译.编译.汇编.链接.所以,首先就应该清楚的首要问题就是:预编译只是对程序的文本起作用,换句话说就是,预编译阶段仅仅对源代码的单词进行变换,而不是对程序中的变量.函数等. 预编译指令的基本知识不作详细介绍,只稍作汇总,重点是后面的我能想到的 使用时的注意事项. 1. 基本内容 预编译指令基本分类如下 类别 指令 预定义符号__FILE__.__LINE__.__DATE__.__TIME__.__STDC__宏#define文件包含#inclu

mbed 程序的语言基础

mBed程序采用C++进行编写,并在此基础上添加了一些自定义的函数和常量,所以我们在这有必要简单地了解一下相应的语言基础. 常量 常量是在程序运算过程中不变的量,常量在程序中经常直接出现,如数字1768:字符‘m’:字符串“mbed”等,此时只要求它们符合相应类型数据的表示方法.相应于各种数据类型,有整型常量.浮点型常量.字符型常量及字符串常量.有时为了代码编写的方便,我们会用一个标识符来代表一个常量,通过宏定义预处理指令来实现,这就是常量定义,格式如下:#define 标识符常量 由用户命名的

我想要的程序开发语言特性&mdash;&mdash;之&ldquo;面向对象&rdquo;&mdash;&mdash;之&ldquo;退化&rdquo;

先从一个例子开始讲起,以下是jdk1.7中的迭代器接口的代码(去掉了注释的部分): public interface Iterator<E> {    boolean hasNext();    E next();    void remove();} 程序开发的老油条们都不太喜欢这个接口的remove方法,原因可能是: 我们为自己实现Iterator接口时,基本不需要这个方法,但我们却不得不@Override它,此时通常我们会直接在方法体中写一行throw new UnsupportedOp

漫谈程序员系列:让程序员蛋疼的那些事儿

听说嫁人要嫁程序员,钱多话少死得早.这话多半是程序员自己黑自己的.程序员是有非常特别的幽默感的一群,善于自嘲,勇于自黑,耐受力超强,很多事无可无不可,不到是不可孰不可忍不会冲冠一怒.不过,就是这么 nice 的人,也很有一些受不了的事儿. 需求变化 为什么把"需求变化"排第一呢? 因为有人说:杀一个程序员不需要用枪,改三次需求就可以了. 由此可见,需求变化的杀伤力有多强. 我见过不少程序员,和产品经理(需求人员)关系紧张,话不投机半句多,或者关公秦琼比脸红.在程序员看来,往往需求一句话

NEWLAND(新大陆)PT850/PT853系列盘点机程序

NEWLAND(新大陆)PT800系列盘点机(PT850.PT83)盘点机程序. 实现: 1,无名称快速采集 2,带名称价格等信息显示盘点,一键下载对接管理软件. 内有配置大全,近30款软件基础信息对接,自己慢慢看. 3,条码采集器模式 4,防伪防串货采集模式,白酒家电种子等行业必须. ------------------------------------ 能输出:固定字长TXT,无空格TXT,XLS表格模式, -----------------------------------------

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署

这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第五篇:MVC程序中实体框架的Code First迁移和部署 原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 译文版权所有,谢绝全文转载--但您可以在您的网站上添加到该教程的

SharePoint 2013 图文开发系列之应用程序页

原文:SharePoint 2013 图文开发系列之应用程序页 在SharePoint中,有两种页面类型,一种是保存在数据库中的页面,我们可以在网站的页面库中看到:还有一种叫做应用程序页,部署在服务器上,Layouts下面的页面,是应用程序页,主要是完成特定功能的页面. 特定功能的页面,听起来比较笼统,举个例子,比如说图片上传到特定文档库,文档批量迁移,权限管理等. 1.添加新建项目,选择SharePoint 2013 空项目,如下图: 2.选择场解决方案,如下图: 3.添加新项,选择应用程序页