Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query on type casting. 1

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi Everybody,
I Have one basic query on 'c' kind of type casting. i wanted to understand how type casting is handled at the compiler level. meaning what provisions are made and how the variable casted is treated by the compiler ???

thanks in advance...

sanjay
 
handled at the compiler level? What is it?
Some examples:
Code:
/* Low-level i/o sample */
typedef struct Hdr { int x, y; } *Phdr;
char buff[100];
Phdr p;
...
if (fread(buff,1,100,f) >= sizeof(struct Hdr))
{
   p = (Phdr)buff; 
   /* as usually, do nothing (char* bits == p bits) 
    * char[] implicitly casted to char* then your cast...
    * Now the compiler knows that you know what to do...
    */
... 
/* Another case (may be you have specifications;) */
int n = 123;
...
printf("%f\n",(double)n); /* as usually, the compiler emits implicit RTL call */
May be it helps?
Or read books on theory of compilation...
 
i wanted to understand how type casting is handled at the compiler level. meaning what provisions are made and how the variable casted is treated by the compiler ?
A cast is an operator that performs a conversion. The compiler generates any code necessary to perform the conversion.
C99 said:
Preceding an expression by a parenthesized type name converts the value of the expression to the named type. This construction is called a cast.
Converting from floating point to integer is one example. The compiler might generate code the calls a function ("__ftol" perhaps) to perform the conversion.

Other conversions might be more trivial, such as converting an int to a char. The compiler might generate code that uses a "word" instruction or a "byte" instruction on the same memory location.

And yet other conversions, such as between pointers to differing data types, may appear to do nothing at all except get rid of a diagnostic message. This need not be the case since the representation of pointers to different types need not be the same.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top