Showing posts with label Data Types. Show all posts
Showing posts with label Data Types. Show all posts

Tuesday, 3 January 2012

Data Types

A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different types of data types (integer, float, character etc.) and need to store the values being used in the program. C language is rich of data types. A C programmer has to employ proper data type as per his requirement.


                           
C has different data types for different types of data and can be broadly classified as
1)Primary data types
2)Secondary data types



1)Primary data types are classified as follows
     a)Character
     b)Integer
     c)Float
     d)Double
     e)Void
              Size & range of the predefined data types
   2)Secondary data types are classified as follows       
         a)
Arrays
         b)Pointers
         c)Structure
         d)Union
         e)Enum etc.

Enum

Defines a set of constants of type int.
The syntax for defining constants using enum is
enum [tag] {name [=value], ...};
The set can optionally be given a type tag name with tag. 
name is the name of a constant that can optionally be assigned the (constant) value of value, etc.
For example,
enum Numbers {One = 1, Two = 2, Three = 3, Four = 4, Five = 5};
If value is missing, then a value is assumed to be the value of the previous constant in the list + 1.
If this is the first constant in the list, the default value is 0. 

Unions

Unions and Structures are identical in all ways, except for one very important aspect. Only one element in the union may have a value set at any given time. Everything we have shown you for structures will work for unions, except for setting more than one of its members at a time. Unions are mainly used to conserve memory. While each member within a structure is assigned its own unique storage area, the members that compose a union share the common storage area within the memory. Unions are useful for application involving multiple members where values are not assigned to all the members at any one time.
Groups variables which share the same storage space.
A union is similar to a struct, except it allows you to define variables that share storage space. 
The syntax for defining unions is:
union [union-type-name]
  {
    type variable-names;
    ...
  } [union-variables] ;
For example,
union short_or_long
  {
    short i;
    long l;
  } a_number;
The compiler will allocate enough storage in a number to accommodate the largest element in the union. 
Elements of a union are accessed in the same manner as a struct. 
Unlike a struct, the variables 'a_number.i' and 'a_number.l' occupy the same location in memory. 
Thus, writing into one will overwrite the other.

Pointers


C pointer is a memory address.
The ‘*’ And ‘&’ Operators
Take a look on the following declaration,
                int x=10;
When we make such declaration how C compiler treat this statement:
1)Reserve memory space for this integer value.
2) Name this memory location as x.
3)Store the value 10 at this location.
This can be illustrated by a simple diagram:

                        Integer variable location in memory.So when we declared an integer variable x and assigned value 10 then compiler occupied a 2 byte memory space at memory address 65325 and stored value 10 at that location. Compiler named this address x so that we can use x instead of 65325 in our program. But we can use address or variable in our program and both will point to the same value (example given below).The memory address of a variable could differ from PC to PC and entirely depends on available free space in memory at time of program execution. As this differs from PC to PC thus we cannot rely on that address (numeric value representing variable address) and we cannot use this address in our program. But have you noticed one thing that address is an integer.We can print address of any variable or function, following program shows how we can do that:Address of variable example.
#include<stdio.h>#include<conio.h> void main(){    int i=9;   
    clrscr();
     printf("Value of i : %d\n",i);    printf("Address of i : %u",&i);     getch();
}
This is a very simple c program which prints value and address of an integer. .
Value at address (*) example
#include<stdio.h>
#include<conio.h>
 void main()
{
    int i=9;
    clrscr();
     printf("Value of i : %d\n",i);
    printf("Address of i : %u\n",&i);
    printf("Value at address of i : %d",*(&i));
     getch();
}
                                                             Output of "value at address" operator example.

Multi Dimensional Array


Syntax:type array_name[d1][d2][d3][d4]………[dn];Where dn is the size of last dimension.
Example:int table[5][5][20];float arr[5][6][5][6][5];In our example array “table” is a 3D (A 3D array is an array of arrays of arrays.) array which can hold 500 integer type elements. And array “arr” is a 5D array which can hold 4500 floating-point elements. Can see the power of array over variable? When it comes to hold multiple values in a C programming, we need to declare several variables (for example to store 150 integers) but in case of array, a single array can hold thousands of values (depending on compiler, array type etc).
Note: To make this multidimensional array example simple I will discuss 3D array for the sake of simplicity. Once you grab the logic how 3D array works then you can handle 4D array or any multidimensional array easily.How to Declaration and Initialization 3D ArrayBefore we move to serious programming let's have a look of 3D array. A 3D array can be assumed as an array of arrays of arrays, it is array (collection) of 2D arrays and as you know 2D array itself is array of 1D array. It sounds a bit confusing but don't worry as you will lead your learning on multidimensional array, you will grasp all logic and concept.
We can initialize a 3D array at the compile time as we initialize any other variable or array, by default an un-initialized 3D array contains garbage value. Let’s see a complete example on how we can work with a 3D array.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int arr[3][3][3]=  
        {
            {
            {11, 12, 13},
            {14, 15, 16},
            {17, 18, 19}
            },
            {
            {21, 22, 23},
            {24, 25, 26},
            {27, 28, 29}
            },
            {
            {31, 32, 33},
            {34, 35, 36},
            {37, 38, 39}
            },
        };
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        for(k=0;k<3;k++)
        {
        printf("%d\t",arr[i][j][k]);
        }
        printf("\n");
    }
    printf("\n");
}
getch();
}
host gator coupon