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:
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:
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:
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:
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: