sizeof operator – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1750 siteicon   siteicon   siteicon  

sizeof operator

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

Learning Objectives

  • Understanding sizeof as an operator.

Source Code

TC++ #1750

 

Source Code

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



Share

CSKC| Created: 16-Dec-2017 | Updated: 31-Aug-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back