Thread: Understanding Strings

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Understanding Strings

    Hy everyone,

    I would like to understand a function on strings --> strlen
    Below is a code that I took from my teacher where the user inputs a string and prints out the length of the string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
        char str[100];
        int i = 0;
        
        printf("Enter String: ");
        scanf("%s", str);
        
        while (str[i]!='\0')
        {
            i++;
        }
        printf("The String is\n%s\n", str);
        printf("Length of the string: %lu\n", strlen(str));
    }
    Now i understand that it returns the count in "int" so my question is:
    Let's say i declared
    Code:
     int count = 0;
    at the beginning of the code
    and then made
    Code:
    count = strlen(str);
    why wouldn't i have the same result?
    Is there a way to do it also?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by YannB View Post
    Let's say i declared
    Code:
     int count = 0;
    at the beginning of the code
    and then made
    Code:
    count = strlen(str);
    why wouldn't i have the same result?
    You would. It's just a shorthand to pass results of functions as arguments to other functions directly.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by GReaper View Post
    You would. It's just a shorthand to pass results of functions as arguments to other functions directly.
    Thank you but when i do that i'm having: implicit conversion loses integer precision: 'unsigned long' to 'int'

    could you explain how can i avoid having that?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    i fixed it!
    but i still don't understand.
    from what i understood i need to declare
    Code:
    long count = 0;
    why do i need to declare long though?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That is compiler dependent. strlen() actually returns a value of type size_t, which is an implementation-defined unsigned integral type.

    If a size_t can represent values that at int cannot, converting it to an int loses information. That is the reason your compiler emits that warning.

    It depends on the range of values an int can represent versus the range of values a size_t can represent - both those ranges are implementation defined. Not all compilers will have a problem.

    There is also no guarantee that a size_t can be converted to a long without losing information.

    The real solution is for count to be given a type of size_t. Don't forget that size_t is an unsigned type.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    Line 11, you use no maximum field width with "%s", and so you will overflow str if a word longer than 99 characters is stored. In addition, you have no guarantee from scanf that a string has been stored at all.

    Line 18, you tell printf to convert an unsigned long with "%lu", but you give it a size_t.

    Below is a fix for these problems. Note that the cast to int is okay as 99, the greatest value that strlen will return, is well within the minimum range that int is guaranteed to have.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
      char s[100];
      if (scanf("%99s", s) == 1)
        printf("%d\n", (int) strlen(s)); 
      return 0;
    }
    If you are wondering what will happen when a word longer than 99 characters is supplied, it will store the first 99 characters and leave the remaining characters on stdin, ready to be read by a following call.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zyxwvuts
    Note that the cast to int is okay as 99, the greatest value that strlen will return, is well within the minimum range that int is guaranteed to have.
    Personally, I think that in principle we should cast from size_t to an unsigned integer type for use with printf, rather than int, even if int will definitely work for the particular case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    C99 has the z length modifier for the size_t type.
    Code:
    printf("%zu", strlen(str));
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding strings and arrays. Specific questions included.
    By yaraeovento in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2013, 02:58 PM
  2. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  3. help understanding strings in c++
    By newbc in forum C++ Programming
    Replies: 12
    Last Post: 06-30-2011, 03:45 PM
  4. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM