C puzzles详解【51-57题】

第五十一题

Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....)
题目讲解:

参考:
http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/
int Add(int x, int y)

{

    // Iterate till there is no carry  

    while (y != 0)

    {

        // carry now contains common set bits of x and y

        int carry = x & y;  

 

        // Sum of bits of x and y where at least one of the bits is not set

        x = x ^ y; 

 

        // Carry is shifted by one so that adding it to x gives the required sum

        y = carry << 1;

    }

    return x;

}
int Add(int x, int y)

{

    if (y == 0)

        return x;

    else

        return Add( x ^ y, (x & y) << 1);

}

 

第五十二题

How do you print I can print % using the printf function? (Remember % is used as a format specifier!!!)
题目讲解:

参考:
http://www.geeksforgeeks.org/how-to-print-using-printf/
printf("%%");

printf("%c", '%');

printf("%s", "%");
 
  

第五十三题

What's the difference between the following two C statements? 

  const char *p;

  char* const p;
题目讲解:
const char *p:
p指向的值只读;
char* const p:
p的值只读;

第五十四题

What is the difference between memcpy and memmove?
题目讲解:
对重叠区域(overlapping regions)的处理有区别。

第五十五题

What is the format specifiers for printf to print double and float values?
题目讲解:
double: %lf
float: %f

第五十六题

Write a small C program to determine whether a machine's type is little-endian or big-endian.
题目讲解:
参考:
http://www.geeksforgeeks.org/little-and-big-endian-mystery/
unsigned int determine_endian()

{

unsigned int i = 1;

char *c = (char *)&i;

if (*c)

        return 0;//little endian

else

        return 1;//big endian

}

 

第五十七题

Write a C program which prints Hello World! without using a semicolon!!!
题目讲解:
#include <stdio.h>



int main()

{

while(printf(“Hello World!”)<0)

{}

}
 
  

 

 
  

 

 
  

 

 
  

 

 
  

 

 
  

 

 
  

 

 
  

你可能感兴趣的:(详解)