News & UpdatesProgrammingWeb programmingPartnersStore
Book Cover
Buy Now
Projects
Links

C++ Tutorial – 05 – Variables II

Integer types

There are four integer types you can use depending on how large a number you need the variable to hold.

char  myChar  = 0; // -128   to +127
short myShort = 0; // -32768 to +32767
int   myInt   = 0; // -2^31  to +2^31-1
long  myLong  = 0; // -2^31  to +2^31-1

Many C++ compilers also support the long long data type, which is guaranteed to be at least 64-bits. This data type is included in the Microsoft C++ compiler.

long long myL2 = 0; // -2^63 to +2^63-1

To determine the exact size of a data type you can use the sizeof operator. This operator returns the number of bytes that a data type occupies in the system you are compiling for.

std::cout << sizeof(myChar)  // 1 byte (per definition)
          << sizeof(myShort) // 2
          << sizeof(myInt)   // 4
          << sizeof(myLong)  // 4
          << sizeof(myL2);   // 8

The Microsoft C++ compiler features five sized integer types. These types start with __int followed by the number of bits you want the integer to hold – either 8, 16, 32 or 64 bits.

__int8   myInt8   = 0; // 8 bits
__int16  myInt16  = 0; // 16 bits
__int32  myInt32  = 0; // 32 bits
__int64  myInt64  = 0; // 64 bits

Signed and unsigned integers

By default, all the number types in Microsoft C++ are signed and may therefore contain both positive and negative values. To explicitly declare a variable as signed the signed keyword can be used.

signed char  myChar  = 0; // -128   to +127
signed short myShort = 0; // -32768 to +32767
signed int   myInt   = 0; // -2^31  to +2^31-1
signed long  myLong  = 0; // -2^31  to +2^31-1
signed long long myL2= 0; // -2^63  to +2^63-1

If you only need to store positive values you can declare integer types as unsigned to double their upper range.

unsigned char  myChar  = 0; // 0 to 255
unsigned short myShort = 0; // 0 to 32767
unsigned int   myInt   = 0; // 0 to 2^32-1
unsigned long  myLong  = 0; // 0 to 2^32-1
unsigned long long myL2= 0; // 0 to 2^64-1

Signed and unsigned may be used as standalone types, which are short for signed int and unsigned int.

unsigned uInt; // unsigned int
signed sInt;   // signed int

Similarly, the short and long data types are abbreviations of short int and long int.

short myShort; // short int
long myLong;   // long int

Floating-point types

The floating-point types can store real numbers with different levels of precision.

float  myFloat  = 3.14; // 3.4E +/- 38 (7 digits)
double myDouble = 3.14; // 1.7E +/- 308 (15 digits)
long double myLongDouble = 3.14; // same as double

The precision shown above refers to the total number of digits in the number. For example, trying to assign more than 7 digits to a float means that the least significant digits will get rounded off.

myFloat = 12345.678; // rounded to 12345.68

Floats and doubles can be assigned by using either decimal or exponential notation.

myFloat = 3e2; // 3*10^2 = 300

Char type

The char type is commonly used to represent ASCII characters.

char c = 'x'; // assigns 120 (ASCII for 'x')

The conversion between the number stored in the char and the character shown when the char is printed occurs automatically.

std::cout << c; // prints 'x'

For another integer type to be displayed as a character it has to be explicitly cast to char. An explicit cast is performed by placing the desired data type in parentheses before the variable or constant that is to be converted.

int i = c;            // assigns 120
std::cout << i;       // prints 120
std::cout << (char)i; // prints 'x'

Bool type

The bool or Boolean type can only either be true or false.

bool b = false; // true or false value

If you like this tutorial please +1 it:


2 Responses

January 28th, 2012 at 06:53  

what happens if i declare a variable as unsigned and assign a negative value???

is there any negative values for character???

January 29th, 2012 at 14:17  

I had expected the compiler to stop you but apparently it doesn’t. It will allow you to assign a negative value to an unsigned variable.

The compiler will assign the bit pattern representing the negative value (in 2′s compliment) to the unsigned int, which will be a large unsigned number. For example, assigning -5 to an unsigned 32-bit int will assign the value 2^32 – 5 or 4294967291.

As for chars, they cannot hold negative values.

Leave a Reply