×

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

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

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.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

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.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.