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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章