data types in c++ with description

data types in c++ with description

  • Additional Integer Types
  • Floating point Types
  • Character Types
  • Enum Types

Additional Integer Types

C++ supports several other integer types. The type short int, which may be written as just short, represents integers that may occupy fewer by memory than the int type. If the short type occupies less memory, it necessarily must represent a smaller range of integer values than the int type. The C++ standard does not require the short type to be smaller than the int type; in fact, they may represent the same set of integer values. The long int type, which may be written as just long, may occupy more storage than the int type and thus be able to represent a larger range of values.

short int ≤ int ≤ long int ≤ long long int 
unsigned short ≤ unsigned ≤ unsigned long ≤ unsigned long long 

C++ Integer Types 

Characteristics of Visual C++ Integer Types AndroWep-Tutorials
Characteristics of Visual C++ Integer Types AndroWep-Tutorials

Within the source code, any unadorned numerical literal without a decimal point is interpreted as an intliteral;

int x = 4456; 
long x = 4456l; 

the literal value4456is anint.

Use the LL suffix for long long literals.

Floating-point Types

Many computation altasks require numbers that have fractional parts. Fore xample, the for mula from mathematics to compute the area of acircle given the circle’ sradius, involves the valueπ, which is approximately 3.14159. C++ supports such non-integer numbers, and they are called floating-point numbers.

  • float 4 bytes 1.17549×10−38 3.40282×10+38 6 digits
  • double 8 bytes 2.22507×10−308 1.79769×10+308 15 digits
  • long double 8 bytes 2.22507×10−308 1.79769×10+308 15 digits
#include <iostream>
int main() 
{ 
    double pi = 3.14159; 
    std::cout << "Pi = " << pi << '\n'; 
    std::cout << "or " << 3.14 << " for short" << '\n'; 
}

Constants

In Listing 3.9 (scientificnotation.cpp), Avogadro’s number and the speed of light are scientific constants; Constants are declared like variables with the addition of theconstkeyword:

const double PI = 3.14159;
PI = 2.5;
#include <iostream>
int main() 
{ 
    const double avogadros_number = 6.022e23, c = 2.998e8; 
    std::cout << "Avogadro's number = " << avogadros_number << '\n'; 
    std::cout << "Speed of light = " << c << '\n'; 
}

Characters

American Standard Code for Information Inter change(ASCII)character set. Standard ASCII canre present 128 different characters. Table 3.4 lists the ASCII codes for various characters.

char ch = 'A';
ch = "A";
#include <iostream>
int main() 
{ 
    char ch1, ch2;  
    ch1 = 65; ch2 = 'A'; 
    std::cout << ch1 << ", " << ch2 << ", " << 'A' << '\n'; 
}
  • ‘\n’—the newline character •
  • ‘\r’—the carriage return character •
  • ‘\b’—the backspace character •
  • ‘\a’—the “alert” character (causes a “beep” sound or other tone on some systems) •
  • ‘\t’—the tab character •
  • ‘\f’—the formfeed character •
  • ‘\0’—the null character

Enumerated Types

C++ allows a programmer to create a new, very simple type an dlistall the possible values of that type. Such a type is called an enumerated type, oran enumeration type. The enum keyword in troduces an enumerated type.

enum Color { Red, Orange, Yellow, Green, Blue, Violet };
enum Color { Red, Orange, Yellow, Green, Blue, Violet };

Exercises

  • Will the following lines of code print the same thing? Explain why or why not
std::cout << 6 << '\n'; 
std::cout << "6" << '\n';
  • Will the following lines of code print the same thing? Explain why or why not.
std::cout << x << '\n'; 
std::cout << "x" << '\n';