C/C++ Crash Course – Data types
Primitive data types in c++ are the variables we store the data for our program. They come in many different flavors and there’s a few key notes to make in this section. Without going in to too much detail about twos-compliment. The first and foremost question is the data going need to be negative in value. If is does then your gonna want to use a signed variable and if the data is only ever going to start at 0 and go up you can use that extra signed bit to store bigger numbers. Here’s a quick block of code showing how to initialize the primitive data types and print out their values and width in bytes (8 bits per byte) to the console.
#include <iostream>
#include <iomanip>
using namespace std;// Allows us to easily access objects and members in the std namespace
int main()
{
// Boolean type
bool boolean = true;// bools can be either true or false
// Integer types (signed)
char character = -127;// chars can be -128 to 127
char characterA = 'A';// or any ASCII Character
short int shortInt = 32767;// shorts can be -32768 to 32767
int integer = 2147483647;// ints can be -2147483648 to 2147483647
long long int longInt = 9223372036854775807;// long integers can be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
// Integer types (unsigned)
unsigned char ucharacter = 250;// unsigned chars can be 0 to 255
unsigned short int ushortInt = 65500;// unsigned shorts can be 0 to 65,535
unsigned int uinteger = 42651203;// unsigned ints can be 0 to 4294967295
unsigned long long int ulongInt = 181147483647;// unsigned long integers can be from 0 to 18,446,744,073,709,551,615
// Floating point types
float decimal = 3.14159265358979323846264;// floating point value +/- 3.4 ^ +/- 38 around 7 digits or precision
double bigdecimal = 3.14159265358979323846264;// Double this big guy can have up to +/- 1.7 ^ +/- 308 around 15 digits or precicion
cout << "bool is " << sizeof(bool) << " bytes wide and can = " << true << " or " << false << endl << endl;
cout << "char is " << sizeof(char) << " bytes wide and can = " << characterA << " or " << (int)character << endl;
cout << "short int is " << sizeof(short int) << " bytes wide and can = " << shortInt << endl;
cout << "int is " << sizeof(int) << " bytes wide and can = " << integer << endl;
cout << "long int is " << sizeof(long int) << " bytes wide and can = " << longInt << endl << endl;
cout << "unsigned char is " << sizeof(unsigned char) << " bytes wide and can = " << (unsigned int)ucharacter << endl;
cout << "unsigned short int is " << sizeof(unsigned short int) << " bytes wide and can = " << ushortInt << endl;
cout << "unsigned int is " << sizeof(unsigned int) << " bytes wide and can = " << uinteger << endl;
cout << "unsigned long int is " << sizeof(unsigned long int) << " bytes wide and can = " << ulongInt << endl << endl;
cout << "float is " << setprecision(7) << sizeof(float) << " bytes wide and can = " << decimal << endl;
cout << "double is " << setprecision(15) << sizeof(double) << " bytes wide and can = " << bigdecimal << endl;
cin.get();
return 0;
}
And the output for that code is:
bool is 1 bytes wide and can = 1 or 0
char is 1 bytes wide and can = A or -127
short int is 2 bytes wide and can = 32767
int is 4 bytes wide and can = 2147483647
long int is 4 bytes wide and can = 9223372036854775807
unsigned char is 1 bytes wide and can = 250
unsigned short int is 2 bytes wide and can = 65500
unsigned int is 4 bytes wide and can = 42651203
unsigned long int is 4 bytes wide and can = 181147483647
float is 4 bytes wide and can = 3.141593
double is 8 bytes wide and can = 3.14159265358979
Now This code is extremely messy and is more an intro in to the limits of each primitive type. I did, how ever, sneak a new helpful lines in there and I’ll be going over it quickly here.
using namespace std;
If you remember in the last tutorial when ever we wanted to use cout or cin we had to append the namespace like this std::cour or std::cin. the using namespace statement effectively pulls those objects into our current namespace.
Note:
I did a few C style casts ((int)character
)as well in order to get the number value from a char. Chars are usually used to store a character, but they can be used just like any other type of integer data type as well. The cout object interprets chars and characters so its easier to print strings and messages with arrays of this type.