C语言中一个小问题

#include <stdio.h>
#include <string.h>


int main()
{
   char send_buf[] = "Hello,client, This is what I want to send you";
   char recv_buf[] = "Really? I don't want to recv it anymore,so don't send it to me";


   if(strlen(send_buf) > strlen(recv_buf))
   {
        printf("the length of send_buf is longer than recv_buf\n");
   }
   else
   {
        printf("the length of recv_buf is longer than send_buf\n");
   }




   if(strlen(send_buf) - strlen(recv_buf) > 0)
   {
        printf("the length of send_buf is longer than recv_buf\n");
   }
   else
   {
        printf("the length of recv_buf is longer than send_buf\n");
   }
   return 0;
}
输出结果:
the length of recv_buf is longer than send_buf
the length of send_buf is longer than recv_buf


在网路程序中使用的是第二种写法,结果一直不是想要的,好奇怪,然后写了这个小东西测试下。
在man strlen一下,知道其中的原因后。


再次写了个测试:
#include <stdio.h>
#include <stddef.h>


int main()
{
   size_t a = 10;
   size_t b = 15;
   size_t c = a - b;
   int d = (c-0)>0?1:0;
   printf("d=%d\n",d);
   return 0;
}
测试结果:
1


发出来分享下,回顾下基础的知识。
发布了160 篇原创文章 · 获赞 5 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章