Data Types

  • Data type is defining an attribute to the variable.
  • It defines the set of legal data the variable can store. The basic data types are int, char, and float.
  • Integer, the data type ‘int’ are whole numbers with a range of values supported by a particular machine. For instance, in a 16 bit word length machine, the integer values lie between -32768 to 32767.
  • ‘C++’ facilitates some control over the integer data type by providing sub data types namely short int, int, long int.
  • Short int represents fairly small integer values and requires half the amount of storage as a normal int uses.
  • Similarly a long int represents fairly higher integer values and requires generally twice the number of bits as a normal int uses. Nevertheless these are highly machine dependent
  • Another option in integer data type is declaring it as unsigned. This unsigned integer uses all bits for the magnitude of the number and is always positive. For instance, in a 16 bit machine the range of unsigned integer is 0 to 65,535. Thus long and unsigned are intended for increasing the range of values.
  • Floating point type: Floating point numbers are numbers that have a decimal point. This data type in ‘C++’ is an attribute for real numbers.
  • The corresponding declaration is, float a;
  • which instructs the compiler that the variable ‘a’ belongs to the data type real. If you want to initialize the variable, then
  • float a;
  • a = 14.752;
  • This can also be achieved through a single statement. float a = 14.752;
  • The keyword float defines the floating point number.
  • When more accuracy is required, another sub data type, double under the float data type can be used.
  • Obviously this double data type uses twice the storage as that of the float data type (8 bytes).
  • To display a double value the format specifier %f is used to obtain the standard floating point notation and %e for scientific or exponential notation
  • Character data type: The char keyword defines a character data type. Thus the declaration for this is char x;
  • x = ‘a’;
  • The variable x is of type character and is initialized to the character ‘a’. The same effect could be achieved as,
  • char x = ‘a’;


Declarations

  • The modern programming languages clearly separate the data and the control statements.
  • All the data is declared first. This makes the program more readable and also provides the information to the compilers to allocate memory spaces.
  • The data and control are well separated by making the former to precede textually, in the name of declarations. Such declarations follow the syntax,
  • type identifier v1, v2......vn; Example: int n1, n2, n3; Example: float x, y, z;
  • Initialization of the data is also possible at the time of declaration. Example: int x =72;
  • Data declaration does two significant tasks for each variable. They are:
  1. Tells the compiler what the variable name is.
  2. Specifies what type of data the variable will hold.


Variables and constant << Previous

Next >> Input and output operations

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus












Footer1