Originally Posted by
Satya
Thank you for the reply. Can you please tell me in which cases i have to use type casts specially for constants.
Some functions, like fgetc, getchar, use type casting to compare the value being returned with EOF. Which is why you normally store the return value in type int. If you instead store them in type char, the comparison with EOF will never succeed.
There are probably better examples, but generally you type cast for comparisons and other things that wouldn't work otherwise. Take this compare function, part of a bubble sort program than does multiple data types:
Code:
int compare_long(const void *m, const void *n)
{
long *ml, *nl;
ml=(long *)m;
nl=(long *)n;
return (*ml>*nl);
}
The void values being passed in are cast in the assignment so that the proper long values can be compared.