Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Typecasting in C (http://www.go4expert.com/articles/typecasting-in-c-t26745/)

poornaMoksha 20Sep2011 18:35

Typecasting in C
 
Typecasting is simply a mechanism by which we can change the data type of a variable, no matter how it was originally defined. When a variable is typecasted into a different type, the compiler basically treats the variable as of the new data type.

Example



Lets go for a very simple example :

Code:

#include<stdio.h>

int main(void)
{
        int a=5,b=8;
        float c = 0, d = 0;

        c = a/b;
        printf("\n [%f] \n",c);

        d = (float)a/(float)b;
        printf("\n [%f] \n",d);

        return 0;
}

In the above example, first we divide 'a' by 'b' without typecasting. While in the second attempt we typecast both 'a' and 'b' to float and then divide them.

So, what happens is, in the first attempt, the compiler knows both 'a' and 'b' are integers so it ignores the floating part of the result and stores the result of a/b in a temporary integer variable and then assign the value of this temporary integer variable to 'c'. So despite of 'c' being a float, the final value that 'c' gets is an integer ie '0' in this case.

While in the second attempt, as both 'a' and 'b' are individually typecasted as 'float', so this typecasting makes compiler think as if both 'a' and 'b' are floats, so the compiler retains the floating part of the result and assigns the float value to 'c'.

The output of the above program :
[0.000000]

[0.625000]

Typecasting with pointers



The above example was good enough to know typecasting but its the pointer arithmetic where typecasting really makes huge difference.

Lets understand it with an example :

Code:

#include<stdio.h>

int main(void)
{
        char arr_char[]= {'l','m','n','o','p','q','r','s','t'};
        int arr_int[]= {1,2,3,4,5,6,7,8,9};

        char *ptr_char = arr_char;
        int *ptr_int = arr_int;

        int i = 0;

        printf("\n Output when char ptr is pointing to char array and int ptr to integer array\n");

        for(i=0;i<9;i++)
        {
                printf(" %d ",*ptr_int);
                ptr_int++;
        }
        printf("\n");

        for(i=0;i<9;i++)
        {
                printf(" %c ",*ptr_char);
                ptr_char++;
        }
        printf("\n");


        printf("\n Output when char ptr is pointing to integer array and int ptr to char array without any typecasting\n");

        ptr_int = arr_char;
        ptr_char = arr_int;

        for(i=0;i<9;i++)
        {
                printf(" %d ",*ptr_char);
                ptr_char++;
        }
        printf("\n");

        for(i=0;i<9;i++)
        {
                printf(" %c ",*ptr_int);
                ptr_int++;
        }
        printf("\n");

        printf("\n Output when char ptr is pointing to integer array and int ptr to char array with typecasting\n");

        ptr_int = arr_char;
        ptr_char = arr_int;

        for(i=0;i<9;i++)
        {
                printf(" %d ",*ptr_char);
                ptr_char = (int*)ptr_char + 1;
        }
        printf("\n");

        for(i=0;i<9;i++)
        {
                printf(" %c ",*ptr_int);
                ptr_int = (char*)ptr_int + 1;
        }
        printf("\n");

        return 0;
}

the output of the above program comes out to be :

Code:

Output when char ptr is pointing to char array and int ptr to integer array
 1  2  3  4  5  6  7  8  9
 l  m  n  o  p  q  r  s  t

 Output when char ptr is pointing to integer array and int ptr to char array without any typecasting
 1  0  0  0  2  0  0  0  3
 l  p  t    ?  ?     

 Output when char ptr is pointing to integer array and int ptr to char array with typecasting
 1  2  3  4  5  6  7  8  9
 l  m  n  o  p  q  r  s  t

Lets try to understand what happened :
  1. Firstly, we made the character pointer to point character array and integer pointer to point integer array. In the loop we tried to print the character array elements by incrementing character pointer. Since each character in character array occupies one byte and a character pointer also increments one byte when '1' is added to it so every thing went as desired. Same is the case with integer array and integer pointer, an integer occupies 4 bytes and an integer pointer increments by four bytes when '1' is added to it.
  2. Here what we did was, we pointed charcter pointer to integer array and integer pointer to character array. So, lets say when we incremented charcter pointer by '1', it incremented by only one byte. Now since an integer occupies 4 bytes in memory, so this character pointer instead of pointing the next integer (which is 4 bytes away), it points to the second byte of the 1st integer and hence we get whatever value was lying there. Same is the case with integer pointer which jumped 4 bytes when incremented by '1' and hence printed the 4th character in array instead of second.
  3. So, as a solution to the problem we faced in 'B' above, this time whenever we incremented the pointer, we typecasted it according to the values it is pointing to, i.e, if character pointer is pointing to integer, then it is typecasted to integer pointer and then incremented by 1. So now after the typecasting is done, the compiler sees a charcter pointer as an integer pointer and increments it by 4. Same is whith integer pointer, which when typecasted to charcter pointer is treated as character pointer and is incremented only once.

Conclusion



So we saw that through typecasting the programer can give a completely different picture of the memory (being typecasted) to the compiler and the compiler works on the memory thinking of it as of typecasted data type for that particular instruction.

nisha134 26Sep2011 14:31

Re: Typecasting in C
 
typecating convert the int value to the float value......

poornaMoksha 26Sep2011 17:11

Re: Typecasting in C
 
Is this something that you are asking or telling?

Sri Abhijit Barman 27Sep2011 10:28

Re: Typecasting in C
 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
clrscr();
printf("Enter a,b values=");
scanf("%d,%d",&a,&b);
sum=a+b;
printf("The Additional Result is=%d",sum);
getch();
}

poornaMoksha 27Sep2011 10:39

Re: Typecasting in C
 
Quote:

Originally Posted by Sri Abhijit Barman (Post 87354)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
clrscr();
printf("Enter a,b values=");
scanf("%d,%d",&a,&b);
sum=a+b;
printf("The Additional Result is=%d",sum);
getch();
}

Whats the intention here?

Sri Abhijit Barman 27Sep2011 10:42

Re: Typecasting in C
 
Quote:

Originally Posted by nisha134 (Post 87322)
typecating convert the int value to the float value......

Who discover c programming?

shabbir 27Sep2011 10:58

Re: Typecasting in C
 
Quote:

Originally Posted by Sri Abhijit Barman (Post 87359)
Who discover c programming?

See this. Not very difficult thing to be doing it.


All times are GMT +5.5. The time now is 23:05.