types of data used in c programming | Allinonetuts

Types of Data used in C:

C supports several different types of data each of which may be represented differently within the computer’s memory. The basic data types are listed below:

Sno.

     Data Type   

Description

   Memory Requirements  

   Examples  

1.

int

Integer

2 bytes or one word

int i,j;

2.

char

Single Character

1 byte

char ch;

3.

float

floating point number

4 byte

float num;

4.

double

double precision floating point number

8 byte ( 2 words )

double n;

 

Read This: Explaining user-defined data types with Example

The basic data types can be augmented by the use of the data type qualifiers short, long, signed and unsigned. For example, integer quantities can be defined as short int, long int or unsigned int. The Interpretation of a qualified integer data type will vary from one C compiler to another, though there are some common sense relationships. Thus, a short int may require less memory than an ordinary int or it may require the same amount of memory as an ordinary int, but will never exceed an ordinary int in word length. Similarly, a long int may require the same amount of memory as an ordinary int or it may require more memory, but it will never be less than an ordinary int.

Basic Data Types in C:

There are basically four data types in C:

    1. char: A char type is used to hold individual characters and usually occupies 1 byte.
    2. int: An int requires 2 bytes by most compilers, and the minimum and maximum numbers that these 2 bytes can hold are -32, 768 and 32767 respectively. An int is used to store whole integer numbers and can not have fractional values or a decimal point.
    3. float: A float data type is used to store floating point numbers i.e., numbers that can have decimal values (e.g., 8.13). The minimum and maximum numbers that it can hold is approximately 3.4E-38 (i.e., 10 raised to the power of -38) and 3.4E+38 respectively. Most compilers require 4 bytes to store a float and they give a fractional precision of 6 digits.
    4. double: A double data type is also used to store floating point numbers, but it can give a greater precision, of about 15 digits. Most compilers require 8 bytes to store a double. The minimum and maximum numbers that can be represented using a double are 1.7E-308 and 1.7E+308 respectively.

Example :

int persons = 6;
float salary = 4523.29; char ch = “a”;

Modifiers used with Basic Data Types:

The basic data types may have various modifiers preceding them. A modifier, alters the meaning of the base type, to fit various situations more precisely. The modifiers are a new line, bulleted list, long, short, signed and unsigned.

1.    Long Modifier: The long modifier can be applied to int and double data types. It can store numbers up to a maximum and minimum of2,147,483,647 and -2,147,483,648 respectively.

2.    Long Int Modifier: Most compilers require 4 bytes to store a long int. for example, any number, greater than 32767 cannot be stored in an int (occupying 2 bytes). Such a number will have to be defined as a long int.

3.    Short Modifier: The short modifier is used to modify the int data type. Compilers, which have the same memory requirement e.g. 4 bytes (or 32 bits) for int as well as long int, will generally have half the size i.e., 2 bytes for a short int. In such a case, if an int is defined as, short int, then it will occupy only 2 bytes as against 4 bytes, had it been defined as an ordinary int.

But, if a compiler is written so that the compiler requirement is the same for an ordinary int as well as short int e.g. 2 bytes then, it makes no difference whether it is defined as a short int or just an int. The range of numbers that it can store in either case will be the same.

4.    Unsigned Modifier: An unsigned int has the same memory requirements as an ordinary int. However, in the case of an ordinary int, or short int or long int, the left most bit is reserved for the sign. So on a typical 2-byte int, it has a range of+32767 to -32768. But with an unsigned int, all the bits are used to represent the numerical value. Hence, an unsigned int can store a maximum number which is twice as large as an ordinary int. For example, if an ordinary 2-byte int stores a range from +32767 to -32768, then an unsigned int can store a range from 0 to 65535. Of course, an unsigned int cannot store negative numbers.

5.    Signed Modifier: The signed data modifier makes it explicit that the basic data type uses a sign bit. That is, the data type can accommodate both positive and negative numbers.

If a variable is a signed int, it has the same positive and negative range as a regular int. By default, an int is a signed data type.

Example:

short int j;
unsigned char a;
unsigned int i;
short unsigned int i;
long int i;
unsigned long int n;
Hello Viewers,
welcome to the Allinonetuts a Technical tutorial and Programming Blog Maintained by Vikas Kardam, a Web Developer and Blogger From Delhi, India.
What types of data used in C Programming? Explain with Example was last modified: July 11th, 2017 by Vikas Kardam
Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

Average rating:  
 1 reviews

Very well explained Types of Data used in C programming.

Leave a Reply

Your email address will not be published. Required fields are marked *