> -----Original Message-----
> From: UTTARA@yahoogroups.com [mailto:UTTARA@yahoogroups.com] 
> On Behalf Of gyan kumar
> Sent: Friday, June 03, 2005 9:35 AM
> To: UTTARA@yahoogroups.com
> Subject: [UTTARA] SUN Micro system Interview
> 
> 
> Hi,
> 
> Yesterday i have attended SUN interview.
> 
> They have asked some question.
> But i didn't apply some question
> Could anyone help me ?
> 
> 1.
> 
> How do you know my machine(CPU) is 64 bit or 32 bit
> without use of sizeof oerator?
> Could anyone tell me?
> 

Section 2.2, Data Types and Sizes, K&R-II:

    "The intent is that short and long should provide different
    lengths of integers where practical; int will normally be the
    natural size for a particular machine. ..."

It also says that:

    "Each compiler is free to choose appropriate sizes for its 
     own hardware, ..."


From the two paragraphs above, we can derive the fact that in a
64-bit machine, an int can either be 64-bit wide, or it can be 
compiler-chosen 32-bit wide.  My best hunch, therefore, is that
a compiler would listen to K&R and use "natural size of the
machine" as the size of int.

Considering my hunch to be true, the following snippet will then 
find the size of a given machine.

    #include 
    #include 

    {
        int i = 0;
        i = (int) ((char*)(&i+1) - (char*)&i);
        
        if ( 64 ==  i * CHAR_BIT )
            printf ( "This is 64-bit machine.\n" );
        else
        if ( 32 == i * CHAR_BIT )
            printf ( "This is 32-bit machine.\n" );
    }

(With inputs from Raghuveer Murthy)


Another method strikes my mind, and that is as follows:

    #include 
    #include 

    {
        if ( UINT_MAX == 18446744073709551615U ) /* 2^64 */
            printf ( "This is 64-bit machine.\n" );
        else
        if ( UINT_MAX == 4294967295U )  /* 2^32 */
            printf ( "This is 32-bit machine.\n" );
    }


[..]

    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)