Hi all
strlen() defined in string.h return size_t. What is it?
Thank you
Hi all
strlen() defined in string.h return size_t. What is it?
Thank you
What is what? size_t? It's an unsigned integer type capable of holding any result from sizeof.
My best code is written with the delete key.
oops sorry,
Thank you, I didn't know what is size_t.
If size_t is always unsigned why my compiler don't give me a warning with the following code:
Thanks againCode:int len; len=strlen(string);
The only time a C compiler ever gave me a warning concerning the signedness of a integer was if I compared a signed and unsigned variable.
As far as I can tell, C compilers won't stop you from assigning an unsigned number to a signed variable like len, but that doesn't mean there is no problem in doing that. The integer could overflow if the string were long enough (say 2^15 in length). If this is a problem, use unsigned longs or size_ts.
Thank you guys!
The integer might "overflow" in the sense that a positive number might become a negative representation, but the actual bit pattern is guaranteed to be intact. In other words, if you do:
z will always be equal to x. The bits themselves are preserved. The meaning of those bits is not.Code:unsigned int x; int y; unsigned int z; y = x; z = y;
> z will always be equal to x. The bits themselves are preserved. The meaning of those bits is not.
If the bits aren't presented properly, the way they should be, than that is indeed an issue. I'd simply contend that trying to express a number bigger than a container can hold is the very definition of overflow. Whether or not z is equal to x is immaterial. You've merely repeated the solution to the problem.
int is signed by default.
try:
Code:unsigned len; len = strlen(string);
I know I can use unsigned type, but why when using SIGNED type my compiler don't give me an error saying:
Possible lost of data converting unsigned to signed.
Thanks again
Are you using the highest warning level?
> if you read in the entire Shakespeare's works into a single string
All the words, and how many times each is used, are listed here - http://www.opensourceshakespeare.com/concordance/
Factoid of the day
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.