Thread: strlen

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    strlen

    Hi all

    strlen() defined in string.h return size_t. What is it?

    Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    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:

    Code:
    int len;
    len=strlen(string);
    Thanks again

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Thank you guys!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by citizen View Post
    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.
    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:

    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.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > 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.

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    int is signed by default.

    try:

    Code:
    unsigned len;
    
    len = strlen(string);

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    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

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you using the highest warning level?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM