Fundamental C++ Data Types

novatech
By -
0

Hello friends so in this articles we will be talking about Fundamental C++ Data Types, as we know while doing programming in any programming language, we use different variables to store all kind of information. and these variables are nothing more than reserved memory location to store values for that variable. So if we put that in simple words that mean that whenever we create or define a variable in our code

So if we put that in simple words that mean that whenever we create or define a variable in our code we are simply reserving some space in memory for that variable.
So before we take a look at what are data types used in C++ we need to know what is Data Type.

What is Data Type:-




Data Types in any programming language is used to specify what kind of data or what type of value a variable has and what type of mathematical, logical or relational operations can be applied to it without causing any problem in the program, for example, string is used to store text and integer is used to store numbers here is the table for different types:-
Data Type
Used for
Example
StringAlphanumeric charactershello world, Alice, Bob123
IntegerWhole numbers7, 12, 999
Float (floating point)Number with a decimal point3.15, 9.06, 00.13
CharacterEncoding text numerically97 (in ASCII, 97 is a lower case ‘a’)
BooleanRepresenting logical valuesTRUE, FALSE

Primitive Built-in Data Types:-






C++ comes with built-in data types and it also allows the user to define their own data types. Here is the table of Data types in C++ :-
TypeKeyword
Booleanbool
Characterchar
Integerint
Floating pointfloat
Double floating pointdouble
Valuelessvoid
Wide characterwchar_t
Many of the above data types can be modify using these modifiers:-
  • signed
  • unsigned
  • short
  • long

Variable Types and Memory Used:-






This table shows the variable types and how much memory they take to store the value in memory, and the maximum and minimum values that can be stored in these type of variables.

Character Data Types

Data Type (Keywords)DescriptionSizeTypical Range
charAny single character. It may include a letter, a digit, a punctuation mark, or a space.1 byte-128 to 127 or 0 to 255
signed charSigned character.1 byte-128 to 127
unsigned charUnsigned character.1 byte0 to 255
wchar_tWide character.2 or 4 bytes1 wide character

Integer Data Types

Data Type (Keywords)DescriptionSizeTypical Range
intInteger.4 bytes-2147483648 to 2147483647
signed intSigned integer. Values may be negative, positive, or zero.4 bytes-2147483648 to 2147483647
unsigned intUnsigned integer. Values are always positive or zero. Never negative.4 bytes0 to 4294967295
shortShort integer.2 bytes-32768 to 32767
signed shortSigned short integer. Values may be negative, positive, or zero.2 bytes-32768 to 32767
unsigned shortUnsigned short integer. Values are always positive or zero. Never negative.2 bytes0 to 65535
longLong integer.4 bytes-2147483648 to 2147483647
signed longSigned long integer. Values may be negative, positive, or zero.4 bytes-2147483648 to 2147483647
unsigned longUnsigned long integer. Values are always positive or zero. Never negative.4 bytes0 to 4294967295

Floating-point Data Types

Data Type (Keywords)DescriptionSizeTypical Range
floatFloating point number. There is no fixed number of digits before or after the decimal point.4 bytes+/- 3.4e +/- 38 (~7 digits)
doubleDouble precision floating point number. More accurate compared to float.8 bytes+/- 1.7e +/- 308 (~15 digits)
long doubleLong double precision floating point number.8 bytes+/- 1.7e +/- 308 (~15 digits)

Boolean Data Type

Data Type (Keywords)DescriptionSizeTypical Range
boolBoolean value. It can only take one of two values: true or false.1 bytetrue or false

You can also use this table :-

TypeTypical Bit WidthTypical Range
char1byte-128 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-128 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short int2bytes0 to 65,535
signed short int2bytes-32768 to 32767
long int8bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int8bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int8bytes0 to 18,446,744,073,709,551,615
float4bytes+/- 3.4e +/- 38 (~7 digits)
double8bytes+/- 1.7e +/- 308 (~15 digits)
long double8bytes+/- 1.7e +/- 308 (~15 digits)
wchar_t2 or 4 bytes1 wide character

The size defined above is different for different compilers so to check the correct size of the various data types on your computer use this code.
</>
#include <iostream>
using namespace std;

int main() {
 cout << "Size of char : " << sizeof(char) << endl;
 cout << "Size of int : " << sizeof(int) << endl;
 cout << "Size of short int : " << sizeof(short int) << endl;
 cout << "Size of long int : " << sizeof(long int) << endl;
 cout << "Size of float : " << sizeof(float) << endl;
 cout << "Size of double : " << sizeof(double) << endl;
 cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
 return 0;
}
In this example code we are using endl, which is used to insert new line character after every line, and << operator is used to pass multiple values out to the screen. And the sizeof() operator is used to get the size of data types.
This is the output that you are going to see:-
</>
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4

typedef Declarations:






typedef is used to create a new name for existing type. This is the simple syntax to define the new type using typedef:-

</>
typedef type newname;
For example:-
</>
 typedef int num;
This above example is telling the compiler that num is another name for int. And you can use it like this and it is now perfectly legal and creates an integer variable called length:
num length;

Enumerated Types:






The enumerated type declares an optional type name and a set of zero or more identifier that can be used as values of the type.
Here is how we can use it.
</>
 enum enum-name {list of names}var-list;
Suppose we have an enum like the following:
</>
 enum Days {Saturday, Sunday, Tuesday,Wednesday, Thursday, Friday};
I want to create an instance of this enum and initialize it with a proper value, so I do:
Days day = Days.Saturday;
Now I want to check my variable or instance with an existing enum value, so I do:
if (day == Days.Saturday)
 
Thanks and this was all for the Fundamental C++ Data Types so do share and if you want any articles do comment below so we can help you learn.
Tags:

Post a Comment

0Comments

Post a Comment (0)