歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 標准C語言<string.h>中的strlen() 與 VC++ 6.0 中的strlen()

標准C語言<string.h>中的strlen() 與 VC++ 6.0 中的strlen()

日期:2017/3/1 10:00:34   编辑:Linux編程

標准C語言中的<string.h>中的strlen()返回值為unsigned int型:

size_t strlen(chonst char* p);//p 的長度不計結束符。typedef unsigned int size_t; 也就是說size_t 就是unsigned int 類型。

而VC++6.0 中的strlen()返回值為int型:

用VC++ 6.0 編程時,如果加了#include<string.h>則調用標准C語言中<string.h>中的strlen(),返回unsigned int型。如果未添加#include<string.h>則調用 VC++6.0的strlen(),返回int型。

/*習題2.26
開發環境VC++6.0*/
#include<stdio.h>
#include<string.h>

int strLonger(char*,char*);

void main(){
char *s = "d";
char *t = "Id";

printf("%d\n",strLonger(s,t));
}

int strLonger(char *s, char *t){
return strlen(s) - strlen(t) > 0; //在C語言中,當一個無符號數和一個有符號數進行比較運算時,有符號數會被隱含地轉換成無符號數,並假設這兩個數都是非負數,然後進行比較運算。
}
//輸出結果為 1</PRE><BR>
<BR>
<PRE></PRE>
<P></P>
<PRE class=cpp name="code">#include<stdio.h>

int strLonger(char*,char*);

void main(){
char *s = "d";
char *t = "Id";

printf("%d\n",strLonger(s,t));
}

int strLonger(char *s, char *t){
return strlen(s) - strlen(t) > 0;
}
//輸出結果為 0

Copyright © Linux教程網 All Rights Reserved