[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 ;
}
编译结果:
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++的更多相关文章
- [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++编译器是如何区分不同的函数的呢?----是 ...
- [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将无法通过编译 ...
- [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++中有不同的变量 ...
- Golang 入门系列(三)Go语言基础知识汇总
前面已经了 Go 环境的配置和初学Go时,容易遇到的坑,大家可以请查看前面的文章 https://www.cnblogs.com/zhangweizhong/category/1275863.html ...
- C语言的预编译,程序员必须懂的知识!【预编译指令】【预编译过程】
由“源代码”到“可执行文件”的过程包括四个步骤:预编译.编译.汇编.链接.所以,首先就应该清楚的首要问题就是:预编译只是对程序的文本起作用,换句话说就是,预编译阶段仅仅对源代码的单词进行变换,而不是对 ...
- 《玩转D语言系列》二、D语言现状、基本规定和相关资源介绍
这算是本系列文章的一个序吧,主要是为以后的学习做铺垫,文本分为三个部分,第一部分是对于网上一些比较旧的资料的问题的一些更正,当然我也不可能看过所有的资料,难免会有遗漏.第二部分是D语言最基本的规定,第 ...
- 【转】24Cxx 系列EEPROM通用程序及应用
关于I2C 学习的时候介绍得最多的就是24C02 这里存储EEPROM了,但学的时候基本只是讲讲简单的I2C 的总线数据传输而已,即使先gooogle上搜索也绝大部分这这样的文章,很少有说到如何在实际 ...
- 用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署 这是微软官方SignalR 2.0教程Getting Started with En ...
- 微信小程序开发语言的选择
微信使用的开发语言和文件很「特殊」. 小程序所使用的程序文件类型大致分为以下几种: ①WXML(WeiXin Mark Language,微信标记语言) ②WXSS(WeiXin Style Shee ...
随机推荐
- ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
一直用的是SQL 数据库,突然改用Access了,使用起来就是没有SQL 顺畅,老是出来些意想不到的错误.今天用Access做的网站程序进行添加数据,调试了一下午,总是异常…… 提示ACCESS的Sy ...
- ThinkPHP中的模型
ThinkPHP中的模型 1.什么是模型(Model) 模型表示企业数据和业务规则,实际项目开发中,主要实现与数据库进行操作. 2.模型的定义规则 模型类的命名规则是除去表前缀的数据表名称,采用驼峰法 ...
- 【leetcode】15. 3Sum
题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- log tag
Media Player MediaPlayerService NuPlayerDecoder NuPlayer MediaFocusControl Ac ...
- sql中更新数据库用到declare @a in
declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...
- yum最常用的命令
yum是一个用于管理rpm包的后台程序,用python写成,可以非常方便的解决rpm的依赖关系.在建立好yum服务器后,yum客户端可以通过 http.ftp方式获得软件包,并使用方便的命令直接管理. ...
- 一个PHP加密脚本,达到一定免杀效果
<?php /**************************************** *author: L.N. *blog : [url]http://lanu.sinaapp.co ...
- How to force to Fullscreen Form
Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...
- 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法
有一种办法来提高Linux安全性--修改 hosts.allow , hosts.deny 这2个文件来配置 允许某个ip访问, 或者禁止访问. 可以通过这种方式设置限制 sshd 的远程访问, 只允 ...
- vs2010中安装ASP.NET AJAX Control Toolkit
方法一: 第一步 下载Ajax Control Toolkit 进入网址http://ajaxcontroltoolkit.codeplex.com/ 即可下载 第二步 解压下载下来的Ajax Con ...