Write a C program that won’t compile in C++

内容摘自http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/

1) In C++, it is a compiler error to call a function before it is declared. But in C, it may compile (Seehttp://www.geeksforgeeks.org/g-fact-95/)

 1 #include<stdio.h>
 2 int main()
 3 {
 4    foo(); // foo() is called before its declaration/definition
 5 }
 6
 7 int foo()
 8 {
 9    printf("Hello");
10    return 0;
11 }

2) In C++, it is compiler error to make a normal pointer to point a const variable, but it is allowed in C. (See Const Qualifier in 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;
}

3) In C, a void pointer can directly be assigned to some other pointer like int *, char *. But in C++, a void pointer must be explicitly typcasted.

#include <stdio.h>
int main()
{
   void *vptr;
   int *iptr = vptr; //In C++, it must be replaced with int *iptr=(int *)vptr;
   return 0;
}

This is something we notice when we use malloc(). Return type of malloc() is void *. In C++, we must explicitly typecast return value of malloc() to appropriate type, e.g., “int *p = (void *)malloc(sizeof(int))”. In C, typecasting is not necessary.

4) Following program compiles & runs fine in C, but fails in compilation in C++. const variable in C++ must be initialized but in c it isn’t necessary. Thanks to Pravasi Meet for suggesting this point.

#include <stdio.h>
int main()
{
    const int a;   // LINE 4
    return 0;
}
Line 4 [Error] uninitialized const ‘a‘ [-fpermissive]

5) This is the worst answer among all, but still a valid answer. We can use one of the C++ specific keywords as variable names. The program won’t compile in C++, but would compiler in C.

#include <stdio.h>
int main(void)
{
    int new = 5;  // new is a keyword in C++, but not in C
    printf("%d", new);
}

Similarly, we can use other keywords like delete, explicit, class, .. etc.

6) C++ does more strict type checking than C. For example the following program compiles in C, but not in C++. In C++, we get compiler error “invalid conversion from ‘int’ to ‘char*‘”. Thanks to Pravasi Meet for adding this point.

#include <stdio.h>
int main()
{
    char *c = 333;
    printf("c = %u", c);
    return 0;
}
时间: 2024-08-30 05:55:23

Write a C program that won’t compile in C++的相关文章

[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 declarat

java编译命令工具javac

Reads Java class and interface definitions and compiles them into bytecode and class files. Synopsis javac [ options ] [ sourcefiles ] [ classes] [ @argfiles ] Arguments can be in any order: options Command-line options. See Options. sourcefiles One

a new Webcam Api Tutorial in C++ for Windows(Windows Media Foundation)--WMF

Sample source code: http://pan.baidu.com/s/1o60VAEA Foword from: http://www.dreamincode.net/forums/topic/347938-a-new-webcam-api-tutorial-in-c-for-windows/page__st__0%26 Well, A long time ago I introduced a Webcam Tutorial. It was for Video For Windo

Using QEMU for Embedded Systems Development

http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opensourceforu.com/2011/07/qemu-for-embedded-systems-development-part-2/ http://www.opensourceforu.com/2011/08/qemu-for-embedded-systems-development-part-3

iOS下OpenGL ES 3.0编程入门(二 ):画一个简单三角形

上文我们讲解了如何构建一个hello world开发环境,那么这一篇我们就来画一个简单的三角形出来. 首先,我要向大家介绍下opengl es的渲染流程,在2.0之前,es的渲染采用的是固定管线,何为固定管线,就是一套固定的模板流程,局部坐标变换 -> 世界坐标变换 ->观察坐标变换->背面消除->光照->裁剪->投影->视口计算->光栅化,程序员只需要调用固定的api修改一些配置参数就可以完成整个渲染流程了.而到了2.0,固定管线改成了可编程管线,我们对整

OpenGL绘制一个三角形

应该建立一个vertex shader文件和一个pixel shader文件,分别命名为shader.vsh和shader.fsh. shader.vsh: attribute vec3 position; //入参,主程序会将数值传入 void main() { gl_Position = vec4(position,1); //顶点经过投影变换变换后的位置 } shader.fsh: void main() { gl_FragColor = vec4(0.5,0.5,0.5,1); //顶点

学习 java命令

依稀记得自己第一次编译*.java文件,第一次运行*.class文件.但是六七年过去了,现在运行java写的程序更多的是用tomcat这种web容器.最近有个小需求,写一个监控zookeeper集群的报警器,当发现集群中节点发生变化时,发出邮件和短信通知运维人员.如果这么一个功能也写成一个web项目放到tomcat里,就有些杀鸡用牛刀了.于是就写了一个jar项目,用 java -jar 运行.占用资源少不说,部署启动很简单,也不占用访问端口.但也遇到了很多问题,才发现自己对java命令还是一知半

使用shc加密bash脚本程序

摘要以前写看到别人写的脚本用shc加密的,我也有就了解了下. SHC代表shell script compiler,即shell脚本编译器.通过SHC编译过的脚本程序对普通用户而言是不读的,因此如果你想保护你的代码(例如含有密钥),则可以考虑SHC:然而有些人可以通过反向编译的方式破解SHC加密过的脚本.下面我们开始介绍: 一.使用SHC加密bash脚本程序1.下载并编译SHC# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.

Linux Overflow Vulnerability General Hardened Defense Technology

Catalog 1. Grsecurity/PaX 2. Hardened toolchain 3. Default addition of the Stack Smashing Protector (SSP): Compiler Flag: GS 4. Automatic generation of Position Independent Executables (PIEs): System Characteristic + Compiler Flag: ASLR 5. Default to