Sunday, January 21, 2007

Introduction to C++ Programming [Part 3]

Previous Related Posts: Part1 Part2

C++ Data Types

There are two groups of built-in data-types; fundamental types and derived types. The fundamental types represent integers and floating-point numbers. The derived types include arrays, strings, pointers and structures. We’ll first look at fundamental types. The following table gives you the data types available, their memory usage and the range of values they can take.


Data Type

Size (in bytes)

Range of values

unsigned short int

2

0 to 65,535

short int

2

-32,768 to 32,767

unsigned long int

4

0 to 4,294,967,295

long int

4

-2,147,483,648 to 2,147,483,647

int (16 bit)

2

-32,768 to 32,767

int (32 bit)

4

-2,147,483,648 to 2,147,483,647

unsigned int (16 bit)

2

0 to 65,535

unsigned int (32 bit)

2

0 to 4,294,967,295

char

1

256 character values

float

4

1.2e-38 to 3.4e38

Double

8

2.2e-308 to 1.8e308

bool

1

true or false



It should be noted that sizes of the data types vary with the platform. C++ standard does not specify exact sizes for data types, as no one choice is suitable for all computer designs. C++ offers a flexible standard with some guaranteed minimum sizes;

· A short integer is at least 16 bits

· An integer is at least as big as short

· A long integer is at least 32 bits and at least as big as int.

Unsigned types don’t hold negative values. To represent floating-point numbers you can either use decimal representation (e.g.: 234.45) or use E notation (e.g.: 2,3445e+2).

A word about the type bool is in order. This type was recently introduced to C++. Earlier C++ used to interpret nonzero values as true and zero values as false (this is still valid). Now you can use the type bool to represent true and false, and the predefined literals true and false represent those values.


Variables

To store an item of information in a computer, the program needs to keep track of three fundamental properties.

· Where the information is stored

· What value is kept there

· What kind of information is stored

We declare variables to keep track of these properties.

Syntax:

Data_type variable_name; //declare a variable

Variable_name = value; //assign a value

Or

Data_type variable_name = value; //declare and assign a value

e.g.: int iAge;

unsigned int uiCounter;

You can create more than one variable of the same type in one statement.

e.g.: long lSize, lWeight;

int iAge = 30, iID, iCityCode = 501;

The type used in the declaration describes the kind of information, and the variable name represents the value symbolically. The program allocates large enough memory space to hold the data. A variable can hold different values of the same data type during the execution of the program.

Naming Rules:

· The only characters you can use in names are alphabetic characters, digits and the underscore character.

· The first character in a name cannot be a digit.

· Variable names are case sensitive.

· You can’t use a C++ keyword for a name. (e.g.: void, return , main)

· C++ places no limits on the length of a name, and all characters in a name are significant.

C++ is a strongly typed language, meaning that you must declare any variable before it is used in the program.

e.g.:

#include <iostream>

using namespace std;

int main()

{

int iIntValue = INT_MAX; //initialize iIntvalue to max int value

cout << "int takes " <<"\n";

cout << "int takes " <<"\n";

cout << "The max int value is "<<"\n";

return 0;

}

INT_MAX is a symbol defined in limits.h file and represents the largest possible value that an int can take. sizeof() function (defined in the compiler) takes either the data type or variable name and returns the number of bytes it occupies in the memory.

The output would be something similar to the following. (Exact values might vary with the system you are using)

int takes 4 bytes

int takes 4 bytes

The max int value is 2147483647

C++ allows you to create aliases for data types. We use the typedef keyword to do so.

Syntax: typedef type type_name;

e.g.: Creating an alias for the type unsigned short int

typedef unsigned short int USHORT;

Now you can use USHORT instead of unsigned short int.

e.g.: USHORT usMyVariable;

Constants

After you initialize a constant, its value is set; the compiler does not let you subsequently change the value. It is a good programming practice to define constants rather than using numeric or character values. When you want to change the value, you only need to change the constant declaration.

Syntax:

const type name = value;

e.g.: const int DAYS = 7;

The const qualifier is used to indicate that the item is a constant. You can assign a value to a constant only when you declare it.

You can use #define statement (e.g.: #define DAYS 7) to create symbolic constants. But it is better to use this only when it is absolutely necessary. const lets you specify the type explicitly, but #define doesn’t. Further, you can use C++’s scoping rules (which we’ll look at under functions) to limit the definition to particular functions or files.

The C++ enum facility provides an alternative means to const for creating symbolic constants, which we’ll be looking at under derived types.

In the next lesson, we'll be starting from operators and control structures.

No comments: