10/24/2011

Data Types


DATA TYPES
Every variable in C++ can store a value. However, the type of value which the variable can store has to be specified by the programmer. C++ supports the following inbuilt data types:- int (to store integer values), float (to store decimal values) and char (to store characters), bool (to store Boolean value either 0 or 1) and void (signifies absence of information).
Integer data type
Integer (int) variables are used to store integer values like 34, 68704 etc. To declare a variable of type integer, type keyword int followed by the name of the variable. You can give any name to a variable but there are certain constraints, they are specified in Identifiers section. For example, the statement
int  sum;
declares that sum is a variable of type int. You can assign a value to it now or later. In order to assign values along with declaration use the assignment operator (=).
int sum = 25;
assigns value 25 to variable sum.
There are three types of integer variables in C++, short, int and long int. All of them store values of type integer but the size of values they can store increases from short to long int. This is because of the size occupied by them in memory of the computer. The size which they can take is dependent on type of computer and varies. More is the size, more the value they can hold. For example, int variable has 2 or 4 bytes reserved in memory so it can hold 2 32= 65536 values. Variables can be signed or unsigned depending they store only positive values or both positive and negative values. And short, variable has 2 bytes. Long int variable has 4 bytes.
Float data type
To store decimal values, you use float data type. Floating point data types comes in three sizes, namely float, double and long double. The difference is in the length of value and amount of precision which they can take and it increases from float to long double. For example, statement
float average = 2.34;
declares a variable average which is of type float and has the initial value 2.34
Character data type
A variable of type char stores one character. It size of a variable of type char is typically 1 byte. The statement
char name = 'c';
declares a variable of type char (can hold characters) and has the initial values as character c. Note that value has to be under single quotes.
Boolean
A variable of type bool can store either value true or false and is mostly used in comparisons and logical operations. When converted to integer, true is any non zero value and false corresponds to zero.
You can find typical size of all data types here.
KEYWORDS
Keywords are the reserved words in any language which have a special pre defined meaning and cannot be used for any other purpose. You cannot use keywords for naming variables or some other purpose. We saw the use of keywords main, include, return, int in our first C++ program.
IDENTIFIERS
Identifiers are the name of functions, variables, classes, arrays etc. which you create while writing your programs. Identifiers are just like names in any language. There are certain rules to name identifiers in C++. They are:-
  1. Identifiers must contain combinations of digits, characters and underscore (_).
  2. Identifier names cannot start with digit.
  3. Keyword cannot be used as an identifier name and upper case and lower case are distinct.
Identifier names can contain any combination of characters as opposed to the restriction of 31 letters in C.
CONSTANTS
Constants are fixed values which cannot change. For example 123, 12.23, 'a' are constants.
Now it's time to move on to our next tutorial Input values using cin operator.

0 komentar:

Posting Komentar