当前位置: 代码迷 >> 综合 >> some c questions
  详细解决方案

some c questions

热度:8   发布时间:2024-01-12 20:07:58.0
几个有意思的C相关题目

1. When computing the square root of 2 by one variant of Newton 's method, you were surprised to find that the result printed was 1.5. What's the mistake ?

#include <stdio.h>

#define abs( x ) ((x) >= 0 ? (x) : -(x))



double sqrt( double y )

{

    double x = 1.0;             /* initial estimate */

    int error;

 

    if ( y <= 0 ) return 0;      /* ensure positive value */

 

    while ( abs( error = y - x * x ) > .0005 )

        x = x + error / (2*x);  /* adjust estimate */

 

    return x;

}

 

int main()

{

    printf( "sqrt(%d) = %8.3f/n" , 2, sqrt(2) );

    return 0;

}

 


2. You want to calculate the 16 bits crc for first 3 members ( serial_number ,   data, and hw_id ) of nvram . Instead, you get unexpected result. You know for sure that crc16() is correct. What is the mistake?

 

#include <stdio.h>

#include <string.h>

 

typedef unsigned short uint16;

typedef unsigned long uint32;

typedef unsigned char uint8;

 

uint16 crc16(char *p, int size); /* calculate 16 bits crc */

 

typedef struct

{

    uint32   serial_number;   /* 4 bytes serial number */

    uint8    data[16];     /* 16 bytes data */

    uint32   hw_id;    /* 4 bytes  hardware id*/

    uint16   crc16;           /* 2 bytes crc16 */

} nvram;

 

nvram nv;

 

int main()

{

     uint8  i;

 

     memset(&nv, 0x0,sizeof (nv));

    

 

     /* assign serial number */

     nv.serial_number = 0x11112222;

  

     /* assign data */

     for (i = 0; i < sizeof (nv.data); i++)

        nv.data[i] = i;

   

     /* assign hardware id */

     nv.hw_id= 0x00110005;

 

     /* calculate crc16 for serial_number, data, and hw_id */

     nv.crc16 = crc16((char *)&nv, sizeof (nv)-sizeof (nv.crc16));

     return 0;

 

}




3. The function, count_vowels() , which is intended to count the vowels in the input string. When running the program, it is taking a long time to do so. Can you spot the difficulty?

 

int count_vowels( char *s )

{

    int sum = 0;

    for (;;)

    {  

        switch ( *s++ )

        {

        case 'a' :

        case 'e' :

        case 'i' :

        case 'o' :

        case 'u' :

        case 'A' :

        case 'E' :

        case 'I' :

        case 'O' :

        case 'U' :

            sum++;

            continue ;

        default :  

            continue ;

        case '/0' :

            break ;

        }

    }

    return sum;

}

 

int main(void)

{

      count_vowels("abcdefghijklmnop" );

      return 0;

}




 

4. A programmer wrote the following function in an embedded system. Everything works fine until compiler's optimization option is turned on. What are the possible reasons ?

 

// io_reg is a memory-mapped IO register

char send_ack()

{

    extern char *io_reg;

 

    // Wait until the device is ready

    while ( *io_reg == 0 );

   

    // write 'A', 'C', 'K' to the register

    *io_reg = 'A' ;

    *io_reg = 'C' ;

    *io_reg = 'K' ;

           

    // Read the status from the register

    // and return the status to caller

    return (*io_reg);

}


 

5. The programmer expected the test for equality to be true, instead, most compilers test this as not equal. Why?

#include <stdio.h>

#include <stdlib.h>

int main()

{

    Const double three = 3.0;

    double x, y, z;

 

    x = 2 / three;

    y = 5 / three;

 

    z = 7 / three;

 

    if ( x + y == z )

        printf("2/3 + 5/3 == 7/3 /n" );

    else

        printf("2/3 + 5/3 != 7/3 /n" );

   

    return 0;

}




 

6.  Why does this program say that the power, and LED is OFF, when we just turned it ON?

#include <stdio.h>

 

typedef struct

{

    int power:1;

    int fan:1;

    int led:1;

} system;

 

static system s;

 

bool is_power_on()

{

    if ( s.power > 0 )

        return true ;

    else

        return false ;

}

bool is_fan_on()

{

    if ( s.fan > 0 )

        return true ;

    else

        return false ;

}

bool is_led_on()

{

    if ( s.led > 0 )

        return true ;

    else

        return false ;

}  

int main()

{

    s.power = 1;

    s.fan= 0;

    s.led = 1;

 

    if ( is_power_on() )

        printf( "power is ON/n" );

    else

        printf( "power is OFF/n" );

 

    if ( is_fan_on() )

        printf( "fan is ON/n" );

    else

        printf( "fan is OFF/n" );

 

    if ( is_led_on() )

        printf( "led is ON/n" );

    else

        printf( "led is OFF/n" );

   

    return 0;

}