Tuesday, August 9, 2011

C++ DataTypes

What is a data type?

When we wish to store data in a C++ program, such as a whole number or a character, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as the range of values that can be stored and the operations that can be performed on variables of that type.
  • A variable must have data type associated with it. It can have data type like integer, decimal numbers, character etc.
  • The variable of type integer stores integer values and a character type variable stores character value.
  • The primary difference between various data type is their size in memory depending on compilers.
  • These also affect the way they are displayed
  • Data types are reserve words of the language

C++ Fundamental Data Types

C++ provides the following fundamental built-in data types:
  • Boolean
  • Character
  • Integer
  • Floatin-point

Boolean Data Type

The Boolean type can have the value true or false. For example:
bool isEven = false;
bool keyFound = true;
If a Boolean value is converted to an integer value true becomes 1 and false becomes 0.
If an integer value is converted to a Boolean value 0 becomes false and non-zero becomes true.


Note that C++ also provides a string class that has advantages over the use of character arrays.

Character Type Example

charvars.cpp // demonstrates character variables

#include //for cout, etc.

void main()
{
char charvar1 = 'A'; //define char variable as character
cout << charvar1; //display character charvar1 = 'B'; //set char variable to char constant cout << charvar1; //display character }


Floating-Point Types

Floating point types can contain decimal numbers, for example 1.23, -.087. There are three sizes, float (single-precision), double (double-precision) and long double (extended-precision). Some examples:

float celsius = 37.623;
double fahrenheit = 98.415;
long double accountBalance = 1897.23;

The range of values that can be stored in each of these is defined by your compiler. Typically double will hold a greater range than float and long double will hold a greater range than double but this may not always be true. However, we can be sure that double will be at least as great as float and may be greater, and long double will be at least as great as double and may be greater.

typedef

C++ allows the definition of our own types based on other existing data types. We can do this using the keyword typedef, whose format is:

typedef existing_type new_type_name ;

where existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining. For example:
1) typedef char C;
2) typedef unsigned int WORD;
3) typedef char * pChar;
4) typedef char field [50];
In this case we have defined four data types: C, WORD, pChar and field as char, unsigned int, char* and char[50] respectively, that we could perfectly use in declarations later as any other valid type:

Anonymous Unions

In C++ we have the option to declare anonymous unions. If we declare a union without any name, the union will be anonymous and we will be able to access its members directly by their member names. For example, look at the difference between these two structure declarations:

struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
} price; // name price
} book;

struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
}; // have no name
} book;

The only difference between the two pieces of code is that in the first one we have given a name to the union (price) and in the second one we have not. The difference is seen when we access the members dollars and yen of an object of this type. For an object of the first type, it would be:

book.price.dollars
book.price.yen

whereas for an object of the second type, it would be:

book.dollars
book.yen

Once again I remind you that because it is a union and not a struct, the members dollars and yen occupy the same physical space in the memory so they cannot be used to store two different values simultaneously. You can set a value for price in dollars or in yen, but not in both.

Data Types

Name Description Size Range
char character or small integer 1 byte signed:-128 to 127
unsigned: 0 to 255
bool Boolean values 1 byte signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
double Double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits)

Now explanation with examples

Example 2


#include //This is pre-processor directive
void main ( ) //this tells the starting point of your program
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z = x + y ;

cout << " x = " ; //print the text on monitor cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }





No comments:

Post a Comment