Skip to main content

Data Types and modifiers In C++

Data types in C++
Data types in C++

Data Types In C++

In computer programming, a data type or simply type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data. It tells about the size and behavior of data. While writing any code we may need to store our data in different forms and of different size. To do this we need different variables of different data types. e.g if we want to store numbers we may need different data type than that of we required to store alphabets. 

When we store something, it means we are reserving space from computer memory. Different data types occupies different memory spaces. If we don't know about them then we may fail to built robust program. so lets see what are the data types that C++ supports and what are their keywords.

  • Primary(Built-in) Data Types:
  • character
  • integer
  • floating point
  • double floating point
  • boolean
  • wide character
  • User Defined Data Types:
  • Structure
  • Union
  • Class
  • Enumeration
  • Derived Data Types:
  • Array
  • Function
  • Pointer
  • Reference

Modifiers: A modifier is used to alter the meaning of the base type so that it more precisely fits the needs of various situations.

Various Modifiers used in C++ are:

  1. Signed
  2. Unsigned
  3. Long
  4. Short

Now lets see the keywords associated with the different data types in C++.

      Keyword
Data Type
bool
Boolean
char
Character
int
Integer
float
Floating point
double
Double floating point
void
Valueless
wchar_t
Wide character

 The table below shows the different data types with modifiers  along with their size in bytes and range. I have derived these information from different books and internet. However the size and range might vary slightly with the compiler and system you are using.
   
Type
Range
Size in Byte
char
-127 to 127 or 0 to 255
1
unsigned char
0 to 255
1
signed char
-127 to 127
1
int
-2147483648 to 2147483647
4
unsigned int
0 to 4294967295
4
signed int
-2147483648 to 2147483647
4
short int
-32768 to 32767
2
unsigned short int
0 to 65,535
-
signed short int
-32768 to 32767
-
long int
-2,147,483,648 to 2,147,483,647
4
signed long int
same as long int
4
unsigned long int
0 to 4,294,967,295
4
float
+/- 3.4e +/- 38 (~7 digits)
4
double
+/- 1.7e +/- 308 (~15 digits)
8
long double
+/- 1.7e +/- 308 (~15 digits)
8
wchar_t
1 wide character
2 or 4

Size of() operator:

To find the actual size (bytes of memory) occupied by the data type in your system you can use Size of () operator.


 #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;
}

 The output of above code is:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

Comments

Post a Comment

Popular posts from this blog

Understanding KNN(K-nearest neighbor) with example

Understanding KNN(K-nearest neighbor) with example.  It is probably, one of the simplest but strong supervised learning algorithms used for classification as well regression purposes. It is most commonly used to classify the data points that are separated into several classes, in order to make prediction for new sample data points. It is a non-parametric and lazy learning algorithm. It classifies the data points based on the similarity measure (e.g. distance measures, mostly Euclidean distance). Assumption of KNN : K- NN algorithm is based on the principle that, “the similar things exist closer to each other or Like things are near to each other.” In this algorithm ‘K’ refers to the number of neighbors to consider for classification. It should be odd value.  The value of ‘K’ must be selected carefully otherwise it may cause defects in our model. If the value of ‘K’ is small then it causes Low Bias, High variance i.e. over fitting of model. In the same way if ‘K’ is v...

Supervised Machine Learning

Supervised Machine Learning What Is Supervised Learning?  It is the machine learning algorithm that learns from labeled data. After the data is analyzed and learned, the algorithm determines which label should be given to new data supplied by the user based on pattern and associating the patterns to the unlabeled new data. Supervised Learning algorithm has two categories i.e Classification & Regression Classification predicts the class or category in which the data belongs to. e.g.: Spam filtering and detection, Churn Prediction, Sentiment Analysis, image classification. Regression predicts a numerical value based on previously observed data. e.g.: House Price Prediction, Stock Price Prediction. Classification Classification is one of the widely and mostly used techniques for determining class the dependent belongs to base on the one or more independent variables. For simple understanding, what classification algorithm does is it simply makes a decision boundary between data po...

Identifiers in C++ : Rules for an Identifier

Rules for an identifiers Identifiers in C++ : Rules for an Identifier Identifiers: Identifiers are the names given to the different entities of the program such as functions, variables, constant, classes, structures, etc. Since identifiers refers to a particular entity of a program so it must be unique for every entities. Rules for an Identifier: An Identifier can only have alphanumeric characters(a-z , A-Z , 0-9) and underscore (_) . The    starting character of    identifier can only contain alphabet (a-z , A-Z) or underscore (_). Identifiers are case sensitive (Similar is in C). For example  name  and  Name  are two different identifiers. Keywords are not allowed to be used as Identifiers. No special characters or Symbols, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier.