C++ data types and variables

               Variables are used to store information. It reserves memory. To reverse the memory, it needs to specify the data types.

The data types are commonly used are given below….

  • ·       Integer(int)
  • ·       Float number(float)
  • ·       Double float number(double)
  • ·       Character(char)
  • ·       Void(void)
  • ·       wide character(wchar_t)

These data types can be used with these type modifiers.

  • signed
  • unsigned
  • short
  • long

let us create a c++ programs to display the size and range of data types.

C++ program:

#include <iostream>

using namespace std;

int main() {

   cout << "Size of char : " << sizeof(char) <<" byte" << endl;

   cout << "char range is-127 to 127 or 0 to 255"<<endl;

   cout <<"Size of unsigned char : "<<sizeof(unsigned char)<<" byte" <<endl;

   cout <<" unsigned char range is 0 to 255"<<endl;

   cout << "Size of signed char :" <<sizeof(signed char)<< " byte" <<endl;

   cout << "signed char range is -127 to 127"<<endl;

   cout << "Size of int : " << sizeof(int) << " bytes"<< endl;

   cout <<"int range is -2147483648 to 2147483647"<<endl;

   cout << "Size of short int : " << sizeof(short int) << " bytes" << endl;

   cout << "short int range is -32768 to 32767"<<endl;

   cout << "Size of long int : " << sizeof(long int) << " bytes" <<endl;

   cout <<"long int range is -9223372036854775808 to 9223372036854775807 " <<endl;

   cout << "Ranges of float, double and wchar_t depends on the compiler "<<endl;

   cout << "Size of float : " << sizeof(float) << " bytes"<< endl;

   cout << "Size of double : " << sizeof(double) << " bytes" <<endl;

   cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" << endl;

   return 0;

}

Output:

Size of char : 1 byte

char range is-127 to 127 or 0 to 255

Size of unsigned char : 1 byte

 unsigned char range is 0 to 255

Size of signed char :1 byte

signed char range is -127 to 127

Size of int : 4 bytes

int range is -2147483648 to 2147483647

Size of short int : 2 bytes

short int range is -32768 to 32767

Size of long int : 8 bytes

long int range is -9223372036854775808 to 9223372036854775807

Ranges of float, double and wchar_t depends on the compiler

Size of float : 4 bytes

Size of double : 8 bytes

Size of wchar_t : 4 bytes

Hope, this code illustrates the basic data types, its size and range. It helps you to understand the concept. Keep coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.