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

 

 
 

 

 
 

 

 
 

 

 
 

 

 
 

 

 
 

 

 
 

转载于:https://www.cnblogs.com/tanghuimin0713/p/3989444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值