Typecasting in C

Discussion in 'C' started by poornaMoksha, Sep 20, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    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 :
    • 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.
    • 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.
    • 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.
     
  2. nisha134

    nisha134 Banned

    Joined:
    Sep 7, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    typecating convert the int value to the float value......
     
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Is this something that you are asking or telling?
     
  4. Sri Abhijit Barman

    Sri Abhijit Barman New Member

    Joined:
    Sep 27, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    #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();
    }
     
  5. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Whats the intention here?
     
  6. Sri Abhijit Barman

    Sri Abhijit Barman New Member

    Joined:
    Sep 27, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Who discover c programming?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    See this. Not very difficult thing to be doing it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice