Home » C++ programming language

sizeof() Operator Operands in C++ programming

C++ sizeof() operator: Here, we are going to learn about the sizeof() operator operands in C++ programming language.
Submitted by IncludeHelp, on February 21, 2020

sizeof() operator

In the C and C++ programming language, sizeof() operator is used to get the size of a data type, size of an expression/variable. It accepts a parameter (either data type or expression) and returns the size of the operator.

sizeof() operands can be:

  1. Data type
  2. Expression/Variable

1) Data type as an operand of sizeof() operator

#include <iostream>
using namespace std;

int main()
{
    cout << "size of char: " << sizeof(char) << endl;
    cout << "size of short: " << sizeof(short) << endl;
    cout << "size of int: " << sizeof(int) << endl;
    cout << "size of long: " << sizeof(long) << endl;
    cout << "size of float: " << sizeof(float) << endl;
    cout << "size of double: " << sizeof(double) << endl;
    cout << "size of long double: " << sizeof(long double) << endl;
    return 0;
}

Output

size of char: 1
size of short: 2
size of int: 4
size of long: 8
size of float: 4
size of double: 8
size of long double: 16

2) Expression/Variable as an operand of sizeof() operator

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    float b = 10.23f;
    double c = 10.23;
    char name[] = "Hello world!";

    //sizeof(variable)
    cout << "size of a: " << sizeof(a) << endl;
    cout << "size of b: " << sizeof(b) << endl;
    cout << "size of c: " << sizeof(c) << endl;
    cout << "size of name: " << sizeof(name) << endl;

    //sizeof(expression)
    cout << "size of 10+10: " << sizeof(10 + 10) << endl;
    cout << "size of a+1: " << sizeof(a + 1) << endl;
    cout << "size of a++: " << sizeof(a++) << endl;
    cout << "size of \"Hello\": " << sizeof("Hello") << endl;

    return 0;
}

Output

size of a: 4
size of b: 4
size of c: 8
size of name: 13
size of 10+10: 4
size of a+1: 4
size of a++: 4
size of "Hello": 6






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.