Categories: C++

Data type and Modifiers in C++

A data type specifies the type of data a variable can store, for example, integer, floating point, character, etc. C++ supports the following data types:

  • Primary or Built-in or Fundamental data type
  • Derived data types
  • User-defined data types
Primary or Built-in or Fundamental data type:

These data types are built-in or predefined data types that the user can use to declare variables directly. These are:

IntegerBoolean
CharacterDouble Floating Point
Floating PointWide Character
Valueless or Void
Derived data types:

Derived Data Types are data types that are derived from primitive or built-in data types. These can be classified into four types:

FunctionArray
ReferencePointer
User-defined data types:

These data types are specified by the user. The following user-defined datatypes are available in C++:

ClassStructure
UnionTypedef defined Datatype
Enumeration
Memory Size and Range of Primitive Data Types:

Let’s take a look at fundamental data types. Its size is based on a 32-bit operating system.

Data TypesMemory SizeRange
char1 byte-128 to 127
signed char1 byte-128 to 127
unsigned char1 byte0 to 127
short2 byte-32,768 to 32,767
signed short2 byte-32,768 to 32,767
unsigned short2 byte0 to 32,767
int2 byte-32,768 to 32,767
signed int2 byte-32,768 to 32,767
unsigned int2 byte0 to 32,767
short int2 byte-32,768 to 32,767
signed short int2 byte-32,768 to 32,767
unsigned short int2 byte0 to 32,767
long int4 byte
signed long int4 byte
unsigned long int4 byte
float4 byte
double8 byte
long double10 byte
Enum Datatype in C++:

An enumerated type specifies an optional type name as well as a set of zero or more identifiers that can be used as type values. Each enumerator is a constant of the enumeration’s type.

The keyword enum must be used when creating an enumeration. An enumeration type’s general form is

enum enum-name { list of names } var-list; 

For instance,

enum day(monday, tuesday, wednesday, thursday, friday) d;
Modifiers in C++:

In C++, modifiers are used to change or add meaning to database types. It is used as a prefix to change the meaning of primitive data types. A modifier is used to alter the meaning of a basic type so that it better meets the needs of different situations. Following are the four datatype modifiers in C++,

  • signed
  • unsigned
  • long
  • short
Important Points to remember:
  • To integer base types, the modifiers signed, unsigned, long, and short can be applied.
  • Furthermore, for char, signed and unsigned can be used, and for double, long can be used.
  • Signed and unsigned modifiers can also be used as prefixes to long or short modifiers. Unsigned long int, for example.
  • Unsigned, short, and long integers can be declared using a shorthand notation in C++.
  • Without int, you can simply use the words unsigned, short, or long. It implies int automatically.
  • Size hierarchy : short int < int < long int
  • The size hierarchy for floating point numbers is: float < double < long double

Note: also read about the OOPs Concepts in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

26 minutes ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago