关于strlen的问题
书上说使用strlen必须含入cstring头文件
#include <iostream>
#include <iomanip>
// #include <cstring> // 可是我注释掉这个头文件还是可以使用strlen,为什么呢?谢谢!
using namespace std;
int main()
{
const int charWidth = 128;
char usrName[charWidth];
cout << "Please enter your name: " << endl;
cin >> setw(charWidth) >> usrName;
if (strlen(usrName)<4)
cout << "Your name should contain at least 4 letters! \n" << endl;
cout << "Hello! " << usrName << ". Nice to meet you!" << endl;
return 0;
}
而且strlen返回的值是size_t strlen( const char *string );
这里size_t是什么类型的?是unsigned int型的吗? 如果是无符号的整型这里if (strlen(usrName)<4)做了强制转换吗?
这种转换有没有问题?
谢谢!
[解决办法]
1. 说明<iostream>间接包含过string.h
2.从int向unsigned int的转换总是成功的.
[解决办法]
typedef unsigned int size_t;
[解决办法]
2. if (strlen(usrName) <4) 这里做了强制转化是从unsigned int向int转换吧? 不是strlen(usrName)的返回值和整数4比较吗?
strlen返回的是unsigned int, 在它和4比较的时候, 4被提升为unsigned int