Data Types - C P P

Theory


  • dataType var => Variable Declaration
  • static dataType var = value => Static variables have a property of preserving their value even after they are out of their scope
  • dataType var = value => Variable declared and Initialized, Created in memory Stack
  • dataType var(value)
  • var = value => Initialization of values
  • Constant
    • Can't be modified
    • Constant Variable
      • const dataType var = value => Needs to be initialized
    • Pointer Variables
    • Functions
    • Object Methods

Primitive


  • Integer
    • Size = 4 byte
    • Unsigned Range = 0 to 232-1
    • Signed Range = -231 to 231-1 => By Default is Signed
  • Float
    • Size = 4 byte
    • Stores up to 7 Decimal digit
  • Double
    • Size = 8 byte
    • Stores up to 15 Decimal digit
  • Character
    • Size = 1 byte
    • Written inside Single quote
  • Boolean
    • Size = 1 byte
    • true = 1 (or > 1), false = 0

Derived


  • Function
  • Array
  • Pointer
  • Reference
    • int& j = i => Same memory with different name

User Defined


  • Class
  • Structure
  • Union
  • Enum => Associate names with integral values
        enum Color {
            RED,
            GREEN,
        };
        Color selectedColor = GREEN;
    

Type Modifiers


  • signed
    • Size = 4 byte
  • unsigned
    • Size = 4 byte
  • long
    • Size = 8 byte
  • short
    • Size = 2 byte

Storage Class


  • Basic
    • A storage class defines scope, default initial value, and a lifetime of a variable
  • Types
    • auto => Automatic Variables
      • By default like local variable, Scope is inside the function, Takes garbage value as default, Lifetime is until the function block's end
    • static => Static
      • By default initialized as 0, Scope is inside the function, Lifetime is throughout the program
      • Value will not be retained outside of the function but the Variable can be used
      • Can only be initialized by Constant literal
    • register => Register
      • Same as Auto storage class
      • It requests the CPU's register memory instead of the local memory for fast access
    • External Variables
      • Like global variable declared outside the function, Scope is global, Default initial value is 0, Lifetime equals the lifetime of the program
    • Mutable
Share: