×

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

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just clarify a few here.

1. The way in which character literals are handled in C and C++ differs. 'x' , 'y'.., and other character literals are interpreted as characters in C++ but as integers in C. Details are provided here.

As an illustration, the following program outputs sizeof(int) in C but sizeof(char) in C++.

C Program:

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

Output:

4

C++ Program:

#include<iostream>
using namespace std;
int main ()
{
cout << sizeof ('x');
return 0;
}

Output:

1

2. The struct label must be used each and every time when a struct variable is declared in C. Struct tags are not required in C++. Let's say that there is a structure for kids. For kids variables in C, we need to use 'struct kids'. We can only use "kids" in C++ instead of the struct.

The program that follows, which generates various outputs in both C and C++, is based on the fact. Sizeof (int) and sizeof (struct k) are printed in C and C++, respectively.

C Program:

#include <stdio.h>
int k;
int main ()
{
	struct k { 
		double y 
	}; // This k covers up the global variable k in C++, but not in C.
	printf ( "%d", sizeof (k) );
	return 0;
}

Output:

4

C++ Program:

#include <iostream>
using namespace std;
int k;
int main ()
{
	struct k { 
		double y;
	}; // This k covers up the global variable k in C++, but not in C.
	cout << sizeof (k) ;
	return 0;
}

Output:

8

3. In C and C++, different boolean result types are found.

C Program:

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

Output:

4

C++ Program:

#include <iostream>
using namespace std;
int main ()
{
	cout << sizeof ( 2==2 );
	return 0;
}

Output:

1

Related Topics

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.