C++ Data Types C-PLUS-PLUS

C++ Data Types  

C++ Data Types

C++ Data Types

The data type specifies the size and type of information the variable will store:

Data Type Size Description
int 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or ASCII values

Example

#include <iostream>
#include <string>
using namespace std;
 
int main () {
  // Creating variables
  int myNum = 5;               // Integer (whole number)
  float myFloatNum = 5.99;     // Floating point number
  double myDoubleNum = 9.98;   // Floating point number
  char myLetter = 'D';         // Character
  bool myBoolean = true;       // Boolean
  string myString = "Hello";   // String
   
  // Print variable values
  cout << "int: " << myNum << "\n";
  cout << "float: " << myFloatNum << "\n";
  cout << "double: " << myDoubleNum << "\n";
  cout << "char: " << myLetter << "\n";
  cout << "bool: " << myBoolean << "\n";
  cout << "string: " << myString << "\n";
 
  return 0;
}
 

Output:

int: 5
float: 5.99
double: 9.98
char: D
bool: 1
string: Hello

Scientific Numbers

A floating point number can also be a scientific number with an "e" to indicate the power of 10:

Example

#include <iostream>
using namespace std;
 
int main () {
  float f1 = 35e3;
  double d1 = 12E4;
  cout << f1 << "\n";
  cout << d1;
  return 0;
}
 

Output:

35000
120000

Booleans

A boolean data type is declared with the bool keyword and can only take the values true or false. When the value is returned, true = 1 and false = 0.

Example

#include <iostream>
using namespace std;

int main() {
  bool isCodingFun = true;
  bool isFishTasty = false;
  cout << isCodingFun << "\n";
  cout << isFishTasty;
  return 0;
}
 

Output:

1
0

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'b':

Example

#include <iostream>
using namespace std;
 
int main () {
  char myGrade = 'D';
  cout << myGrade;
  return 0;
}
 

Output:

D

Char datatype with ASCII values

You can also use ASCII values to display certain characters.

Example

#include <iostream>
using namespace std;
 
int main () {
  char a = 65, b = 66, c = 67;
  cout << a;
  cout << b;
  cout << c;
  return 0;
}
 

Output:

ABC

ASCII values 65 to 90 for uppercase A to Z

ASCII values 97 to 122 for lowercase a to z

Strings

The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double-quotes.

To use strings, you must include an additional header file in the source code, the <string> library.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string greeting = "Hello";
  cout << greeting;
  return 0;
}
 

Output:

Hello

Download free E-book of C-PLUS-PLUS


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries