C++ Data Types

October 06, 2015 Posted by WithU Technologies
Data Type is used to specify the proper use of identifier, which kinds of operands can be performed and what type of data can be stored within a variable.

System allocates some memory based on the data type specified by the programmer. It also specifies the type of data which is stored in the reserved memory.

Numeric Data Types:

Integers (whole numbers): 

· Used as: int (int a=19;)
· It used non-fractional numbers which can be positive or may be negative.
· Example: -78, 58

Types of Integer modifiers:



Floating Point (Decimal):

· Used as: float (double a=1.09;)
· Example: 3.14, - 6.53

Types of Floating Point modifiers:




Alphanumeric data Types:

Strings:
· It is part of Standard library; thus we need to include it in header as #include <string>.

· But as it already included with <iostream>, so you don’t need to include it separately.

· Composed of numbers, characters and symbols.

· Ordered sequence of characters.

· Literals are placed in between double quotation.

· Example: “Hello”, “We all are equal”

#include <iostream>
using namespace std;

int main(){

string a="king";  // string variable initialized

if(a=="king" || a=="Duke"){ // two condition based on string variable

cout<<"Yes, it is true"<<endl; // Executed: if any one of the condition will matched

   }

}

Characters:
· Used as: char (char x=’R’;)

· Composed of single letter or symbols.

· It holds a 1-byte integer which is interpreted in ASCII (American Standard Code for Information interchange) instead of integer.

· Literals enclosed between single quote.

· Example: ‘x’, ‘a’

Booleans Data Type:
·

· Used as: bool (bool age=true;)

· It only returns two possible values.

· Either True which means 1 or False which signifies 0.


Naming Conventions


· All variable must begin with a letter of the alphabet or an underscore.

· After the initial letter the variable name contains additional letter or numbers.

· Blank Spaces or Special character are not allowed.

· Variables are case sensitive in nature.

· C++ keywords cannot be used as variable names; those keywords are reserved for C++ use.


Camel case: The first letter of an identifier is lowercase and first letter of each subsequent word is capitalized. Example: mainActivity


Pascal case: Both the first letter of an identifier and first letter of each subsequent word are capitalized. Example: MainActivity