gpt4 book ai didi

c - 如何更正我的程序,该程序同时具有三元运算符和按位运算符?

转载 作者:行者123 更新时间:2023-11-30 21:02:03 25 4
gpt4 key购买 nike

/*to print bigger of two numbers using ternary operator*/
#include <stdio.h>
void main()
{int a,b,c;
char p,q;
printf("enter the values of a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
a>b?((a>c)?printf("a is bigger"):printf("c is bigger")):((b>c)?printf("b is bigger"):printf("%d is bigger",c));
/*swap two characters using bitwise operator*/

printf("\n enter p and q:");
scanf("%c%c",&p,&q);
p=p^q;
q=p^q;
p=p^q;
printf("after swapping p =%c,q=%c",p,q);
}

这是我输入的程序。它的第一部分,即三个数字中较大的一个给出了正确的输出,但第二部分,即交换没有发生。输出有点像这样输入 p 和 q:ab交换 p=a,q= 后注意:当我单独执行按位交换程序时,我得到了正确的输出。请提供我的问题的解决方案以及我能做什么

最佳答案

问题似乎不在于您的三元或按位运算符,而在于您的输入。

阅读pq时,应在%c前留一个空格 -

scanf(" %c %c",&p,&q);
/* ^ ^ leave space */

这是因为之前 scanf 中的 '\n' 仍保留在 stdin 中,并被读入 p 中,并且您输入的字符进入 q ,因此交换后您不会获得所需的输出。

See working code .

注意- void main() -> int main(void)int main (int argc,char **argv) .

关于c - 如何更正我的程序,该程序同时具有三元运算符和按位运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com