Data Types in C++ Technical Elite

 Data Types in C++ 



Data Types

                            The data types defines a set of values and set of operation on those values . The computer manipulates various types of data. The data is given to the program as input . It is processed according to the program instruction and output is returned. The data and its types are defined before designing the actual program to process the data. The type of each data value is identified at the beginning of program design  
                                            A C++ program may need to process different types of data . Each data type required different amount of memory. C++ provide the following data types

  • int
  • float
  • double
  • char

Integer Data type

                                           There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables can only hold whole numbers.

Computer programs collect pieces of data from the real world and manipulate them in various ways. There are many different types of data. In the realm of numeric information, for example, there are whole numbers and fractional numbers. There are negative numbers and positive numbers. And there are numbers so large, and others so small, that they don’t even have a name. Then there is textual information. Names and addresses, for instance, are stored as groups of characters. When you write a program you must determine what types of information it will be likely to encounter. If you are writing a program to calculate the number of miles to a distant star, you’ll need variables that can hold very large numbers. If you are designing software to record microscopic dimensions, you’ll need to store very small and precise numbers. Additionally, if you are writing a program that must perform thousands of intensive calculations, you’ll want variables that can be processed quickly. The data type of a variable determines all of these factors. Although C++ offers many data types, in the very broadest sense there are only two: numeric and character. Numeric data types are broken into two additional categories: integer and floating point. Integers are whole numbers like 12, 157, −34, and 2. Floating point numbers have a decimal point, like 23.7, 189.0231, and 0.987. Additionally, the integer and floating point data types are broken into even more classifications. Before we discuss the character data type, let’s carefully examine the variations of numeric data. Your primary considerations for selecting a numeric data type are • The largest and smallest numbers that may be stored in the variable • How much memory the variable uses • Whether the variable stores signed or unsigned numbers • The number of decimal places of precision the variable has The size of a variable is the number of bytes of memory it uses. Typically, the larger a variable is, the greater the range it can hold. 


DATA TYPES SIZE TYPICAL RANGE
short int 2 bytes -32,768 to +32,767
unsigned short int 2 bytes 0 to +65,535
int 4 bytes -2,147,483,648 to +2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long int 4 bytes -2,147,483,648 to +2,147,483,647
unsigned long int 4 bytes 0 to 4,294,967,295
long long int 8 bytes -9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
unsigned long long int 8 bytes 0 to 18,446,744,073,709,551,615

Program

 // This program has variables of several of the integer types.
#include <iostream>
using namespace std;

 int main()
{
 int checking;
 unsigned int miles;
 long days;
 
    checking = -20;
    miles = 4276;
    days = 189000;
    cout << "We have made a long journey of " << miles;
    cout << " miles.\n";
    cout << "Our checking account balance is " << checking;
    cout << "\nAbout " << days << " days ago Columbus ";
    cout << "stood on this spot.\n";
    return 0;
}

Output

We have made a long journey of 4276 miles.
Our checking account balance is -20
About 189000 days ago Columbus stood on this spot.

Char Data Type

The char data type is used to store individual characters. A variable of the char data type can hold only one character at a time. Here is an example of how you might declare a char variable:
                    
                    char letter;

This statement declares a char variable named letter, which can store one character. In C++, character literals are enclosed in single quotation marks. Here is an example showing how we would assign a character to the letter variable: 
    
                    letter = 'g';

This statement assigns the character 'g' to the letter variable. Because char variables can hold only one character, they are not compatible with strings. For example, you cannot assign a string to a char variable, even if the string contains only one character. The following statement, for example, will not compile because it attempts to assign a string literal to a char variable. 

                    letter = "g"; // ERROR! Cannot assign a string to a char

Important that you do not confuse character literals, which are enclosed in single quotation marks, with string literals, which are enclosed in double quotation marks

Program

// This program works with characters.
#include <iostream>
using namespace std;
int main()
{
 char letter;
 letter = 'A';
 cout << letter << endl;
 letter = 'B';
 cout << letter << endl;
 return 0;
}

Output

                A
                B
Although the char data type is used for storing characters, it is actually an integer data type that typically uses 1 byte of memory. (The size is system dependent. On some systems, the char data type is larger than 1 byte.)

ASCII 

The reason an integer data type is used to store characters is because characters are internally represented by numbers. Each printable character, as well as many nonprintable characters, is assigned a unique number. The most commonly used method for encoding characters is ASCII, which stands for the American Standard Code for Information Interchange. (There are other codes, such as EBCDIC, which is used by many IBM mainframes.) When a character is stored in memory, it is actually the numeric code that is stored. When the computer is instructed to print the value on the screen, it displays the character that corresponds with the numeric code. You may want to refer to Appendix B , which shows the ASCII character set. Notice that the number 65 is the code for A, 66 is the code for B, and so on. Program demonstrates that when you work with characters, you are actually working with numbers. 

Program

// This program demonstrates the close relationship between
// characters and integers.
#include <iostream>
using namespace std;  
int main()
{
    char letter;  
    letter = 65;
    cout << letter << endl;
    letter = 66;
    cout << letter << endl;
    return 0;
 }

Output

                A
                B


Comments

Popular posts from this blog

Basics Structure of C++ Technical Elite