Write a program that produces different results in C and C++

http://www.geeksforgeeks.org/write-c-program-produce-different-result-c/



Write a program that compiles and runs both in C and C++, but produces different results when compiled by C and C++ compilers.

There can be many such programs, following are some of them.

1) Character literals are treated differently in C and C++. In C character literals like ‘a’, ‘b’, ..etc are treated as integers, while as characters in C++. (See this for details)

For example, the following program produces sizeof(int) as output in C, but sizeof(char) in C++.

#include<stdio.h>
int main()
{
  printf("%d", sizeof(‘a‘));
  return 0;
}

2) In C, we need to use struct tag whenever we declare a struct variable. In C++, the struct tag is not necessary. For example, let there be a structure for Student. In C, we must use ‘struct Student‘ for Studentvariables. In C++, we can omit struct and use ‘Student‘ only.
Following is a program that is based on the fact and produces different outputs in C and C++. It prints sizeof(int) in C and sizeof(struct T) in C++.

#include <stdio.h>
int T;

int main()
{
    struct T { double x; };  // In C++, this T hides the global variable T,
                            // but not in C
    printf("%d", sizeof(T));
    return 0;
}

3) Types of boolean results are different in C and C++. Thanks to Gaurav Jain for suggesting this point.

// output = 4 in C (which is size of int)
printf("%d", sizeof(1==1)); 

// output = 1 in c++ (which is the size of boolean datatype)
cout << sizeof(1==1); 
时间: 2024-10-05 08:15:55

Write a program that produces different results in C and C++的相关文章

how to use dbx in Solaris -Debugging Your Program With dbx

发现gdb在Linux上挺好用的,但是在我们公司solaris上却不能用,在Solaris上要用dbx.下面就来学习一下如何用dbx. Debugging Your Program With dbx You are likely to be debugging your program for one of the following reasons: To determine where and why it is crashing. Strategies for locating the c

Speculative store buffer

A speculative?store?buffer is speculatively updated in response to speculative store?memory operations buffered by a?load/store?unit in a microprocessor. Instead of performing dependency checking for?load?memory operations among the?store?memory oper

从缓存行出发理解volatile变量、伪共享False sharing、disruptor

volatilekeyword 当变量被某个线程A改动值之后.其他线程比方B若读取此变量的话,立马能够看到原来线程A改动后的值 注:普通变量与volatile变量的差别是volatile的特殊规则保证了新值能马上同步到主内存,以及每次使用前能够马上从内存刷新,即一个线程改动了某个变量的值,其他线程读取的话肯定能看到新的值. 普通变量: 写命中:当处理器将操作数写回到一个内存缓存的区域时.它首先会检查这个缓存的内存地址是否在缓存行中,假设不存在一个有效的缓存行,则处理器将这个操作数写回到缓存,而不

GET DIAGNOSTICS Syntax

http://dev.mysql.com/doc/refman/5.7/en/get-diagnostics.html GET [CURRENT | STACKED] DIAGNOSTICS { statement_information_item [, statement_information_item] ... | CONDITION condition_number condition_information_item [, condition_information_item] ...

matlab实战中一些重要的函数总结

这段时间看了一些大型的matlab工程文件(如:faster r-cnn),对于工程中经常要用到的一些函数进行一个总结. 1.路径问题. 这主要涵括文件路径的包含和组合. curdir = fileparts(mfilename('fullpath')); addpath(genpath(fullfile(curdir, 'utils'))); mkdir_if_missing(fullfile(curdir, 'utils')); caffe_path = fullfile(curdir, '

使用神经网络来识别手写数字【译(三)- 用Python代码实现

实现我们分类数字的网络 好,让我们使用随机梯度下降和 MNIST训练数据来写一个程序来学习怎样失败手写数字. 我们也难怪Python (2.7) 来实现.只有 74 行代码!我们需要的第一个东西是 MNIST数据.如果有 github 账号,你可以将这些代码库克隆下来, git clone https://github.com/mnielsen/neural-networks-and-deep-learning.git 或者你可以到这里 下载. Incidentally, 当我先前说到 MNIS

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

Early 80386 CPUs

Assembling a detailed and accurate history of the 80386, including a complete listing of all the "steppings" (revisions), when they were released, what "errata" (problems) each stepping suffered from, and which of those problems were f

UNREAL ENGINE 4.12 正式发布!下载地址

UNREAL ENGINE 4.12 正式发布! 下载地址:https://www.unrealengine.com/ Alexander Paschall 在 June 1, 2016 |功能新闻社区 Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn 此版本内含虚幻引擎 4 的数百个更新,以及 GitHub 虚幻引擎开发者社区提交的 106 项改良!特此对虚幻引擎 4.12 版本的贡献者们表达诚挚谢意: