C语言的几个有趣问题

问题1. 不能使用分号,编写一个“Hello World”程序。

问题2. 如何用C语言打印“ I am print %”?

问题3. 不能使用">、<、>=、<=“等关系运算符,找到3个整数a ,b ,c中的最小的数。

问题4. 程序中不使用“+”法运算,实现整数加法运算。

 

参考程序:

1. 这是一个比较有趣的问题,但是答案往往十分简单,所采用的是最基本的语句,参考程序如下:

#include <stdio.h>
void main()
{
    if(printf("Hello World!\n")){}  //可以换成while等
}

2. 对于初学者来说,有不少人不知道如何打印百分号这种类型的符号,其实也很简单,无非是在前面加个百分号而已。

#include <stdio.h>
int main()
{
    printf("I am print %%!");
}

3. 第3个问题有一定的挑战性,而且答案不止一个,此外,关系运算符有:

C语言提供6种关系运算符,如表所示:
运算符
名称
示例
功能
缩写
<
小于
a<b
a小于b时返回真;否则返回假
LT
<=
小于等于
a<=b
a小于等于b时返回真;否则返回假
LE
>
大于
a>b
a大于b时返回真;否则返回假
GT
>=
大于等于
a>=b
a大于等于b时返回真;否则返回假
GE
==
等于
a==b
a等于b时返回真;否则返回假
EQ
!=
不等于
a!=b
a不等于b时返回真;否则返回假
NE

 

参考程序:

复制代码
#include <stdio.h>
#define min(a,b) (((a)-(b)) >> 31 ? (a) : (b))
int main()
{
    int a , b , c;
    a = -30;
    b = 59;
    c = 56;
    printf("min = %d",min(min(a,b),c));
}
复制代码

4. 不能使用"+"法运算,自然想到当初学的计算机组成原理的二进制相加的知识,采用位运算来实现相加:

复制代码
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y;   //输入的两个数
    int inBit = 0x0000;
    int tailBit = 0x0001;
    int result = 0,Rbit;
    int XlastBit,YlastBit;
    int inBitREP;
    printf("please input two numbers :");
    scanf("%d%d",&x,&y);
    while(tailBit)
    {
        XlastBit = x&tailBit;
        YlastBit = y&tailBit;
        Rbit = XlastBit^YlastBit^inBit;
        inBitREP = 0;
        if( (XlastBit == tailBit && YlastBit == tailBit) || ( XlastBit == tailBit && inBit == tailBit) || (YlastBit == tailBit && inBit == tailBit))  //超过两个1,则进位为1
        {
            inBitREP = tailBit<<1;
        }
        inBit = inBitREP;
        result = result|Rbit;
        tailBit = tailBit<<1;
    }
    printf("The result is %d\n",result);
    system("pause");
    return 0;
}
复制代码

    以上便是C语言的几个有趣的问题,希望学习C语言的童鞋们会喜欢,以上代码均在CodeBlocks上运行无误。第一次发帖子,求各位大神轻喷!小弟先谢过了!

 

posted on   大卫david  阅读(1166)  评论(29编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示