Table of contents
1.
Introduction
2.
sizeof function
3.
Comparison Operations
3.1.
C
3.2.
C++
4.
Frequently Asked Questions
4.1.
What are comparison operations?
4.2.
What is the sizeof() function?
4.3.
Why do the results of comparison operations in C and C++ differ?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Results of comparison operations in C and C++

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C++ is one of the most widely used programming languages for competitive programming. Learning C++ reinforces essential computer science principles while also providing access to a wide range of professional options.
Comparison operations specify how the system should compare an incoming value supplied as a context parameter to a predetermined value or value range. Instead of being supplied by a context parameter, the test parameter can also be supplied as the result of a nested expression. The sizeof() function is used for comparing the results of the operations in C and C++.

So, let's start with a brief description of the sizeof() function.

Also see : C Static Function

Click on the following link to read further: Features of C Language

sizeof function

In C/C++, the Sizeof() operator is a machine-dependent feature that changes from compiler to compiler. It's possible to call it byte-specific functionality. It aids in the allocation of variables to memory by supplying the byte and size of the variables, as well as the number they occupy. The Sizeof() method is only used to determine the precise size of a variable type in C/C++ programming. The sizeof operator has a return type that returns the total number of bytes in memory. It is particularly beneficial in the implementation and development of portable apps because of its adaptability and flexibility.
Lets understand the working of the sizeof() function with the help of an example.

Consider the code snippet:

#include <stdio.h>
int main(){
    int x=sizeof(50); //Assign the value of size of Integer to x
    int y= sizeof(50.5); //Assign the value of size of Double to y
    printf("The size of Integer is %d \n The size of double is %d", x,y);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

The size of Integer is 4
The size of double is 8

Explanation:
In the code snippet, x is assigned with the value of Integer, and y is assigned the value of Double. Hence based on the compiler, the output is 4 and 8, respectively.

Let's get into the details of the results of comparison operations in C and C++.

You can also read about the jump statement, Tribonacci Series and  Short int in C Programming

Must Read Passing Arrays to Function in C

Comparison Operations

Comparison operations specify how the system should compare an incoming value supplied as a context parameter to a predetermined value or value range. Instead of being supplied by a context parameter, the test parameter can also be supplied as the result of a nested expression.

Let's have a look at some C and C++ examples to understand the different results of the comparison operations in C and C++.

C

In C programming language , the result of comparison operations has the data type int. Take a look at the following example.

#include<stdio.h>
int main()
{
    int a = 28, b = 24;
    printf("%d \n", sizeof(a == b));
    printf("%d \n", sizeof(a < b));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

4
4

Explanation:
In C, the result of comparison operations has an int data type, due to which the result is displayed as 4 (the size of Integer). Try it on online C compiler.

Must Read Decision Making in C

C++

In C++, the result of comparison operations has the data type bool. Take a look at the following example.

#include<iostream>
using namespace std;
int main()
{
    int a = 28, b = 24;
    cout << sizeof(a > b ) << "\n";
    cout << sizeof(a == b);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

1
1

Explanation:
In C++, the result of comparison operations has a boolean data type, due to which the result is displayed as 1 (the size of boolean).

Also check out this article - Pair in C++ and, Dynamic Binding in C++

Must Read what is storage class in c

Frequently Asked Questions

What are comparison operations?

Comparison operations specify how the system should compare an incoming value supplied as a context parameter to a predetermined value or value range. 

What is the sizeof() function?

The sizeof() method is used to determine the precise size of a type of variable in C/C++ programming.

Why do the results of comparison operations in C and C++ differ?

The result of comparison operations in C has an int data type, whereas the comparison operations in C++ have a bool data type.

Conclusion

In this article, we have extensively discussed the results of comparison operations in C and C++. The article explains the variation of the result of comparison operators in C and C++.
We hope that this blog has helped you enhance your knowledge regarding comparison operations in C and C++ and if you would like to learn more, check out our articles on C++. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow. 

C++ vs Java

Happy Coding!!

Live masterclass