×

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

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

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

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.