Like Tree1Likes
  • 1 Post By jimblumberg

size_t in source code of strlen

This is a discussion on size_t in source code of strlen within the C Programming forums, part of the General Programming Boards category; Hi! I read the source code of strlen() function in a book called "The Standard C Library" and it is ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    25

    size_t in source code of strlen

    Hi! I read the source code of strlen() function in a book called "The Standard C Library" and it is as follows:
    Code:
    /* strlen function */ 
    #include <string.h> 
    size_t (strlen)(const char *s) 
    { /* find length of s[] */ 
    const char *sc; 
    for (sc = s; *sc != '\0'; ++sc) 
    { 
    return (sc - s) ; 
    }
    What is size_t here?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    unsigned int

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    25
    Is it a reserved identifier?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    it is an int that can not be negative.Since this is strlen which calculates the length of a string the value the function returns can not be negative.

    What is a reserved identifier?

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    25
    I presume it is a keyword,globally used only by the compiler

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    unsigned int
    No, it is not necessarily an unsigned int.
    7.19 Common definitions <stddef.h>
    1
    The header <stddef.h> defines the following macros and declares the following types.
    Some are also defined in other headers, as noted in their respective subclauses.
    2
    The types are
    ptrdiff_t
    which is the signed integer type of the result of subtracting two pointers;
    size_t
    which is the unsigned integer type of the result of the sizeof operator;
    6.5.3.4 The sizeof and _Alignof operators
    Constraints

    5
    The value of the result of both operators is implementation-defined, and its type (an
    unsigned integer type) is size_t, defined in <stddef.h> (and other headers).
    So it could be any unsigned integer type. Don't assume any exact size. If the function uses size_t then you should use size_t for your variable's type.

    I presume it is a keyword,globally used only by the compiler
    No, it is a type of variable, just like int is a type of variable.

    Jim
    AndiPersti likes this.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    size_t is a typedef in stddef.h.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    25
    That's what i wanted to know..Thanks a lot guys

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I did not know the above! However in this example it is certaintly an unsigned int

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    However in this example it is certaintly an unsigned int
    Why? It could be an unsigned long on some systems, don't assume. Just because your compiler is using unsigned int for size_t doesn't mean every other compiler does. The standard leaves it to the compiler to determine the actual 'size'.

    Jim

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Correct.It is resolved at compilation time.You are tottaly right!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is object code the same as assembly source code?
    By c_weed in forum Tech Board
    Replies: 3
    Last Post: 01-05-2012, 06:25 PM
  2. looking for source code
    By ph071 in forum C Programming
    Replies: 10
    Last Post: 12-07-2008, 10:52 AM
  3. Source code
    By mewatC in forum Windows Programming
    Replies: 3
    Last Post: 09-30-2006, 10:17 PM
  4. Source Code now available
    By Ash1981 in forum C Programming
    Replies: 0
    Last Post: 02-01-2006, 06:52 AM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21