码迷,mamicode.com
首页 > 其他好文 > 详细

C puzzles详解【51-57题】

时间:2014-09-24 00:51:55      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   strong   for   div   

第五十一题

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", "%");

第五十三题

Whats 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 machines 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)
{}
}

 


 


 


 


 


 


 


C puzzles详解【51-57题】

标签:style   blog   http   color   io   ar   strong   for   div   

原文地址:http://www.cnblogs.com/tanghuimin0713/p/3989444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!