Ads

Data Types

DATA TYPES IN C:

- In C programming language it is flexible to assign the amount of memory you want to allocate for a particular variable through data types, i. e, we can use these data types and allocate space in memory depending on our requirement.
-  These data types also define the type of data you want to store in that memory location, like normal integers or numbers with decimal points etc.
-In C data types  are of three types
      1. Primitive
      2. Derived
      3. User Defined
- Primitive: By using primitive datatypes we can create variable (which represents memory) which can store single value.
- Derived : The Datatypes which are derived from primitive datatypes are considered as Derived datatypes. E.g. Arrays, pointers
- User Defined: These kinds of datatypes are defined by user that is programmer. Ex: structure , Union, enum.
- Derived data types and user defined data types are explained in separate chapters.
- There are four types of primitive datatypes. They are:
i) char
ii) int
iii) float
iv) double
- The size and range of these C data types vary from compiler to compiler.
- The data types are mentioned before the variable’s name in Declaration Section.

Syntax:

<Data_type> <variable name>;
- Angled brackets are used for demonstration purpose only and should not be used in your c program.

Examples:
 char c; /*c is a variable name and it is allocated 1 byte of memory of char type*/
int x; /*x is a variable name and it is allocated 4bytes(GCC) of memory of int type*/
float f; /*f is a variable name and it is allocated 4bytes of memory of float type */

- char provides 1 byte of memory and int provides 2 or 4 bytes of memory (compiler dependent), float provides 4 byte of memory and double provides 8 bytes of memory.
- char and int can be used to store integer type of variables i. e, like ……….-3,-2,-1,0,1,2,3,……
But they have their own limits as shown below
- char variable can store -128 to +127 signed values and 0 to 255 unsigned values
And a integer variable stores -2147483648 to +2147483647 (or) -32468 to +32467 depending on the compiler type, explained in below example.
Whereas float and double are used to store the values with decimal points for ex 3.48, thus float and double can be used to store measured values which require a decimal point precision.

Sizeof() operator:

- Using sizeof() operator we can know the size of the datatype for a respective compiler.

Syntax:
sizeof(<datatype>);
sizeof(<variable>); 
- Angled brackets are used for demonstration purpose only and should not be used in your c program.
- Make use of this example and find out the size of the datatypes provided by your compiler.

Program:

/*If you are using Turbo C use this code.*/

#include<stdio.h>
#include<conio.h>

int main()
{
 clrscr();
 printf("\n size of char %d", sizeof(char));
 printf("\n size of int %d", sizeof(int));
 printf("\n size of float %d", sizeof(float));
 printf("\n size of double %d", sizeof(double));
 getch();

 return 0;
}

Output:
   size of char 1
   size of int 2
   size of float 4
   size of double 8

Program:

/*If you are using GCC*/ 

#include<stdio.h>
int main()
{
 printf(" size of char: %d\n", sizeof(char));
 printf(" size of int: %d\n", sizeof(int));
 printf(" size of float: %d\n", sizeof(float));
 printf(" size of double: %d\n", sizeof(double)); 

 return 0;
}


Output:

size of char: 1
size of int: 4
size of float: 4
size of double: 8

- We can also find out the size of variable, as shown in below program.

Program:

/*Program to find out the size of variable of particular type*/ 

#include<stdio.h>
int main()
{
 int i;
 float f;
 char c;
 double d;

 printf(" size of char\t: %d\n", sizeof(c));
 printf(" size of int\t: %d\n", sizeof(i));
 printf(" size of float\t: %d\n", sizeof(f));
 printf(" size of double\t: %d\n", sizeof(d));

 return 0;
}


Output:

  size of char     : 1
  size of int      : 4
  size of float     : 4
  size of double   : 8


NOTE: Did you notice that same program when executed in different compilers they are exhibiting different values, i.e. in Turbo C compiler size of int is 2 but in GNU C Compiler size of int is 4, but all other datatypes have same amount of memory allocated. That is the reason; I was saying the integer size is compiler dependent.
- A more detailed description on the range, memory and format specifiers are given in table below.              
DATA TYPE
MEMORY
FORMAT SPECIFIER RANGE
GCC TC
CHAR 1byte 1 byte  %c Signed:-128 to 127
Unsigned: 0 to 255
INT 4 bytes 2 bytes %d Signed:-2147483648 to 2147483647(GCC) and
-32,468 to 32467(TC)
Unsigned: 0 to 4,294,967,295(GCC) and 0 to 65,535 (TC)
FLOAT 4 bytes 4 bytes %f 3.4E-38 to 3.4E+38
DOUBLE 8 bytes 8 bytes %lf 1.7E-308 to 1.7E+308

Formula to calculate range of given data type
- If it is signed datatype then: -2n-1 to [2n-1-1]     where n is the no of bits allocated for the datatype.
For Unsigned datatype: 0 to 2n-1,    where n is the number of bits, and this formula is applicable to int and char datatypes only.
- Okay! Let’s use the formula for char datatype.
Char provides 1 byte i. e, 8 bits (since 1 byte = 8 bits or 1 byte = 2 nibbles)
so n=8 => for char
=> -2n-1 to 2n-1-1
=> -28-1to 28-1 -1
=> -27to 27-1
=> -128 to 128-1
=>-128 to 127
- That’s it, signed char range is -128 to 127.
- In the similar manner calculate for unsigned char and signed and unsigned int in both TC and GCC compilers.



No comments:

Post a Comment