[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 ...
随机推荐
- iOS开发中遇到的头文件找不到的问题解决办法
有时会遇到莫名其妙的明明有这个文件,但是就是显示头文件找不到,我也是咨询了技术大牛之后知道的这个方法,之后恰巧我一个朋友问我cocoapod加进去之后头文件找不到,我就让他试了下这个方法果然好用,我也 ...
- 滚动视图和页面控制UIScollView,UIpageControlDemo
//// ViewController.m// UIScollView//// Created by hehe on 15/9/25.// Copyright (c) 2015年 wang.h ...
- THREE.js代码备份——canvas_lines(随机点、画线)
<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - l ...
- 引入OO开发报表后的感想
很早就想尝试着在常规的报表开发中 引入OO了 趁着程序略复杂 时间略充裕 终于尝试了一把-咩哈哈~~ 以下来自我的evernote笔记 有点语无伦次-忍忍~~ -------------------- ...
- FIR滤波器(1)- 基础知识
FIR滤波器广泛应用于数字信号处理中,主要功能就是将不感兴趣的信号滤除,留下有用信号.FIR滤波器是全零点结构,系统永远稳定:并且具有线性相位的特征,在有效频率范围内所有信号相位上不失真.在无线通信收 ...
- 转 XMLHttpRequest().readyState的五种状态详解
转 http://javathinker.blog.ccidnet.com/blog-htm-itemid-1262479-do-showone-type-blog-uid-36384.html 在& ...
- 关于Silverlight调用天气预报接口问题
问题:因Silverlight客户端不能直接调用webservice接口(外网天气接口),调用会出现跨域访问的问题,即使添加了跨域文件也不好使.解决方法如下 解决方法一:1.在服务端建立一个wcf服务 ...
- composer安装yii2或者laravel报错
大概的信息就是提示让登陆github,然后就报错了 Could not fetch https://api.github.com/authorizations, enter your GitHub c ...
- How to modify Code Comments[AX2012]
// This is a framework class. Customizing this class may cause problems with future upgrades to the ...
- selenium+python 浏览器标签页跳转 switch_to_window
浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...