×

Program that produces different results in C and C++

Introduction:

There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers.

There are a variety of such programmes, some of which are listed below.

Character literal handling in C and C++:

Both C and C++ have various rules for handling character literals. Character literals like "a," "b," "..etc." are handled as characters in C, while they are treated as integers in C++. Details are provided here.

For instance, the output of the following programme in C++ is sizeof(char), but in C, it outputs sizeof(int).

Program:

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

Explanation:

  • The main() function is the entry point of the program, and it contains the code that will be executed.
  • The printf() function is a standard function that outputs a formatted string to the console.
  •  In this case, "%d" is a format specifier for an integer and sizeof('x') is an expression that returns the size of the character literal 'x' in bytes.

Program Output:

Program that produces different results in C and C++

Struct variable declaration in C and C++:

Every time we declare a struct variable in C, the struct tag must be used. The struct tag is not necessary for C++. As an illustration, imagine that Student has a structure. For Student variables in C, we must use'struct Student'. In C++, we can omit the struct and merely use the term "Student."

The software, based on this fact, generates various outputs in C and C++ are

shown below. It displays the sizeof(int) in C and the sizeof(struct T) in C++.

Program:

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

Explanation:

  • The line int a; declares a global variable a of type integer.
  • The main() function is the entry point of the program, and it contains the code that will be executed.
  • In the main() function, there is a declaration of a structure type named a. This structure has a single member y of type double. The line printf("%d", sizeof(a)); outputs the size of the structure a in bytes using the printf() function and the format specifier %d for an integer.

Program Output:

Program that produces different results in C and C++

Increment operator behaviour in C and C++:

In C, the ++ operator (the increment operator) is a "sequence point" operator. This means that when it is used in an expression, any side effects (such as modifying a variable) are guaranteed to happen before the next expression is evaluated. This means that when the ++i expression is used in the first printf statement, the value of i is incremented to 1 before the printf statement is executed, so it prints "1". Similarly, i is still 1 when the second printf statement is executed, so it also prints "1".

In C++, the ++ operator does not have this behaviour. The language defines that the order of evaluation of sub-expressions is unspecified, which means that the behaviour of the ++i expression is undefined.

In practice, most C++ compilers will still follow the C behaviour and increment the value of i before the printf statement is executed, so the code will print "1" followed by "2". However, this is not guaranteed by the C++ language standard, and a different compiler or optimization settings could produce different results.

It's worth noting that in c++, ++i and i++ have different behaviour. i++ returns the current value of i, while ++i returns the incremented value of i.

Program:

#include <stdio.h>


int main() {
    int i = 0;
    printf("%d\n", ++i); // prints 1 in C and 2 in C++
    printf("%d\n", i); // prints 1 in C and 2 in C++
    return 0;
}

Explanation:

  • The main() function is the entry point of the program, and it contains the code that will be executed.
  • In the main() function, the line int i = 0; declares a local variable i of type integer and initializes it with the value 0.
  • The first call to printf() outputs the value of ++i. The operator ++ is a pre-increment operator and increments the value of i before using it. In C and C++, the expression ++i evaluates to 1, and the printf() function outputs this value to the console.
  • The second call to printf() outputs the value of i, which has already been incremented by the first call to printf(). In both C and C++, the expression i evaluates to 1, and the printf() function outputs this value to the console.

Program Output:

Program that produces different results in C and C++

Global variable behaviour in C and C++:

In C, variables declared at the global scope (outside of any function) are by default treated as "extern" variables, meaning they are visible to all functions in the program.

In C++, global variables are by default treated as "static" variables, meaning they have internal linkage and are only visible within the file where they are declared.

Program:

#include <stdio.h>
int x = 0;


void func() {
    x = 5;
}


int main() {
    x = 10;
    printf("%d\n", x); // prints 10 in C and C++
    func();
    printf("%d\n", x); // prints 5 in C and 10 in C++
    return 0;
}

Explanation:

  • The line int x = 0; declares a global variable x of type integer and initializes it with the value 0.
  • The func() function is a user-defined function that sets the value of x to 5.
  • The main() function is the entry point of the program, and it contains the code that will be executed.
  • In the main() function, the line x = 10; sets the value of the global variable x to 10. The first call to printf() outputs the value of x, which is 10, and the printf() function outputs this value to the console.
  • The line func(); calls the func() function, which sets the value of x to 5. The second call to printf() outputs the value of x, which is 5 in C and 10 in C++, and the printf() function outputs this value to the console.

Program Output:

Program that produces different results in C and C++

Note:

In C, the func() function can modify the global variable x because it has global scope. In C++, global variables have internal linkage by default, meaning that they can only be accessed from within the same translation unit. To modify a global variable from a function, the variable must have an external linkage.

To make this code produce the same results in C and C++, we would need to explicitly declare the global variable as an extern in C++:

extern int x = 0;

Or add an extern keyword before the variable in the main function,

extern int x;

This would tell the C++ compiler to treat the variable as having external linkage so that the func function modifies the same global variable as in C.

Differences in Constructor-Destructor Chain Behavior Between C and C++:

In C++, when an object of a derived class is created, the constructor of the base class is called first, and when the object is destroyed, the destructor of the derived class is called first, and then the destructor of the base class is called. This is called the "constructor-destructor chain", and it is a feature of C++.

However, in C, there is no concept of constructors and destructors. There is no chain of constructor-destructor calls and no mechanism to automatically call them when an object is created or destroyed.

In C, when an object is created, the memory is allocated, and the object's members are initialized with default values. When an object is destroyed, the memory is deallocated, but there is no mechanism to call any destructor.

Program:

#include<iostream>


class A {
 public:
  A() {std::cout << "A constructor" << std::endl;}
  ~A() {std::cout << "A destructor" << std::endl;}
};


class B: public A {
 public:
  B() {std::cout << "B constructor" << std::endl;}
  ~B() {std::cout << "B destructor" << std::endl;}
};


int main() {
  B b;
  return 0;
}

Explanation:

  • This program defines two classes, A and B, using the class keyword. Class A has a default constructor and a default destructor, both of which are defined using the C++ member function syntax (A::A() and A::~A()).
  • Class B inherits from class A using the public inheritance mechanism. Class B also has a default constructor and a default destructor, both of which are defined using the C++ member function syntax (B::B() and B::~B()).
  • In the main() function, an instance of class B is declared using the B b; statement.
  •  When this statement is executed, the constructor of class A is called first and writes "A constructor" to the standard output stream.
  •  Then the constructor of class B is called and writes "B constructor" to the standard output stream.
  •  When the b object goes out of scope at the end of the main() function, the destructor of class B is called and writes "B destructor" to the standard output stream, followed by the destructor of class A and writing "A destructor" to the standard output stream.

Program Output:

Program that produces different results in C and C++

Related Topics

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

C++ Exception Handling

Exception is an unexpected problem that occurs at program run time. This problem might include condition such as division by zero, running out of memory space, array out of bonds, etc....

2 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.