C语言 #define ABS(X) (X >= 0) ? X : -X 遇到的逻辑错误

本文讲述了作者在编程中遇到的问题,通过宏定义ABS函数的实例,揭示了逻辑运算符优先级的重要性。通过修改宏定义,解决了if语句的异常行为,并解释了为什么输出结果是-8。关键点在于理解运算符的结合性和优先级规则。
摘要由CSDN通过智能技术生成

本想对一个数进行取绝对值处理,于是写了这个宏定义:

#define ABS(X) (X >= 0) ? X : -X

后来当我写了一个这样的判断语句时:

if (i == ABS(num))

发现只要 num 的值不为 0 ,if 后的的语句永远是真命题
于是写了两行代码把判断的结果作为整型输出试试:

	int num = 8;
	printf("%d", 5 == ABS(num));

发现结果并非 0 或 1 ,而是一个 -8
把句子展开之后才恍然大悟:
在这里插入图片描述
原来是?前的逻辑运算优先级要比 ? : 高
所以将宏定义改成下面这样就可以了:

#define ABS(X) (X >= 0 ? X : -X)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 150 struct ENode { int V1,V2; }; typedef struct ENode *bian; struct AdjVNode { int subscript; struct AdjVNode *next_subscript; }; typedef struct AdjVNode *spot; typedef struct headAdjVNode { int head_spot; spot next_spot; } H[MAX]; struct GNode { int Nv; int Ne; H G; }; typedef struct GNode *list; struct ey { int x,y; }; typedef struct ey eryu; eryu zoubiao[MAX]; int visit[MAX]; list creat(int sum); void gojian(list head,int num,int sum); void charu(list head,int left,int right); void bianli(list head,int now_spot,int num); int main() { int sum,num; scanf("%d%d",&sum,&num); list tu; tu=creat(sum); gojian(tu,num,sum); // for(int i=0;i<=sum;i++){ // printf("%d:",i); // for(spot tran=tu->G[i].next_spot;tran;tran=tran->next_subscript) // printf(" %d",tran->subscript); // printf("\n"); // } if(tu->G[0].next_spot==NULL) { printf("No\n"); }else if(num+7.5>=50){ printf("Yes\n"); } else { bianli(tu,0,num); printf("No\n"); } return 0; } list creat(int sum) { list head; head=(list)malloc(sizeof(struct GNode)); head->Nv=sum; head->Ne=0; for(int i=0; i<=sum; i++) { head->G[i].head_spot=i; visit[i]=0; head->G[i].next_spot=NULL; } return head; } void gojian(list head,int num,int sum) { zoubiao[0].x=0,zoubiao[0].y=0; for(int i=1; i<=sum; i++) scanf("%d%d",&zoubiao[i].x,&zoubiao[i].y); for(int i=1; i<=sum; i++) { int goudu=sqrt(pow(zoubiao[i].x,2)+pow(zoubiao[i].y,2)); if(goudu<=(7.5+num)) charu(head,0,i); } for(int i=1; i<sum; i++) { for(int j=i+1; j<=sum; j++) { if(sqrt(pow((zoubiao[i].x-zoubiao[j].x),2)+pow((zoubiao[i].y-zoubiao[j].y),2))<=num) charu(head,i,j); } } } //创建边 void charu(list head,int left,int right) { bian tran; spot spot_tran; spot_tran=(spot)malloc(sizeof(struct AdjVNode)); tran=(bian)malloc(sizeof(struct ENode)); tran->V1=left; tran->V2=right; spot_tran->subscript=right; spot_tran->next_subscript=head->G[left].next_spot; head->G[left].next_spot=spot_tran; spot_tran=(spot)malloc(sizeof(struct AdjVNode)); spot_tran->subscript=left; spot_tran->next_subscript=head->G[right].next_spot; head->G[right].next_spot=spot_tran; } void bianli(list head,int now_spot,int num) { if(50-abs(zoubiao[now_spot].x)<=num || 50-abs(zoubiao[now_spot].y)<=num){ printf("Yes\n"); exit(0); } visit[now_spot]=1; for(spot tran=head->G[now_spot].next_spot;tran;tran=tran->next_subscript){ if(visit[tran->subscript]==0) bianli(head,tran->subscript,num); } }
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值