Detailed Print – CSKC
Sign In

Search

siteicon
Code Learning#CPP#1750 Back

sizeof operator

Datatype size is found by using the sizeof operator in c++

Learning Objectives

  • Understanding sizeof as an operator.

Source Code

		 

Click to open popup

Run Output

Size of integer=4
Size of character=1

Code Understanding

int n=10; int si=sizeof(n);
Here si will contain the size which is occupied in memory by the variable n. Since n is an integer variable, it will occupy 4 bytes in most modern operating systems. In older systems it was 2 bytes.

char c=’a’; int sc=sizeof(c);
Here sc will contain the size occupied by variable c, which has earlier been defined as a character type variable. A single char is always defined as 1 byte.

Notes

  • Most modern compilers also support the char16_t and char32_t declaration which are 2 byte and 4 byte respectively.
  • The declaration wchar_t is compiler dependent, which can be 1 byte, 2 byte or more.The sizeof usage for many primitive data types can also be seen from following code sheet.
    Finding Size of CPP Data Types


Suggested Filename(s): sizeofop.cpp, operator-sizeof.cpp