Getting confused with strings and length

This is a discussion on Getting confused with strings and length within the C Programming forums, part of the General Programming Boards category; Hi, could someone explain why I am getting two different values in the code below please? Code: #import <stdio.h> #import ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    Getting confused with strings and length

    Hi, could someone explain why I am getting two different values in the code below please?

    Code:
    #import <stdio.h>
    #import <string.h>
    
    int main (int argc, const char *argv[])
    {
    	int inputLength;
    
    	char userInput[10]; 
    	
    	printf("Enter a number to be reversed: ");
    	scanf("%s", &userInput);
    	
    	inputLength = strlen(userInput);
    
    	printf("inputLength = %i\n\n", inputLength);
    
    	char computerOutput[inputLength];
    
    	printf("\n\ncomputerOutput length = %i", strlen(computerOutput));
    
    	printf("\n\nYour entry of %s is %i characters long.\n\n", userInput, inputLength);
    
    	for (int x = inputLength-1, y = 0; x >= 0; --x, ++y)
    		computerOutput[y] = userInput[x];
    
    	printf("computerOutput = %s\n\n", computerOutput);
    }
    So if I enter 23 at the prompt I get the follwoing output:

    inputLength = 2 (yep this is what I expected)

    computerOutput length = 21 (What? where is 21 coming from? it should be 2)

    Your entry of 23 is 2 characters long. (yep this is what I expected)

    computerOutput = 32??????hw???c? (this is 15 long not 21)

    I am obviously missing the plot here?

    Can someone help please?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since computerOutput is garbage, who knows how long it's going to be? Apparently six of the characters are not printable, even as question marks.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Since computerOutput is garbage, who knows how long it's going to be? Apparently six of the characters are not printable, even as question marks.
    LOL I am not overly worried about the garbage in computerOutput.

    What I dont understand is that if the userInput length is 1after I enter a single digit the variable inputLength is correctly set to .... 1

    But when I then use inputLength as the dimension when declaring computerOutput why does the length of computerOutput be reported as 5 and not 1?

    If I enter a 4 digit entry again inputLength = 4 which is correct but after using it to dimension computerOutput it turns into....21?

    I dont get it?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    It is better to use fgets instead of scanf to read strings.
    Remember: strlen returns the actual length of the string stored inside the buffer, but not the size of the buffer itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Quote Originally Posted by FreeCare View Post
    LOL I am not overly worried about the garbage in computerOutput.
    Well, you should be, because it is the root of your troubles.

    Quote Originally Posted by FreeCare View Post
    But when I then use inputLength as the dimension when declaring computerOutput why does the length of computerOutput be reported as 5 and not 1?
    Answer:
    Quote Originally Posted by tabstop
    Since computerOutput is garbage, who knows how long it's going to be? Apparently six of the characters are not printable, even as question marks.
    Quote Originally Posted by FreeCare View Post
    If I enter a 4 digit entry again inputLength = 4 which is correct but after using it to dimension computerOutput it turns into....21?
    Answer:
    Quote Originally Posted by tabstop
    Since computerOutput is garbage, who knows how long it's going to be? Apparently six of the characters are not printable, even as question marks.
    Quote Originally Posted by FreeCare View Post
    I dont get it?
    Answer:
    Quote Originally Posted by tabstop
    Since computerOutput is garbage, who knows how long it's going to be? Apparently six of the characters are not printable, even as question marks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And also remember that the length of a string is "how many characters there are before a character with all bits zero", which cannot possibly be changed or determined by a declaration of an array.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    char computerOutput[2];

    this would have an array with 2 characters containing garbage until set to something.

    the length of this array is 2.

    int x = 5;

    char computerOutput[x];

    this would have an array with 5 characters containing garbage until set to something.

    the length of this array is 5

    So why does strlen(computerOutput) return .....21 and not .... 5

    This is what I do not understand.

    I know that until all characters are assigned a value they will contain garbage printable or otherwise.

    What I do not understand is why when using a variable with the value of 5 to declare an array size it somehow turns into 21?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    You're talking about buffer size. The entire array consists of 5 elements of type char. So the array size is 5, not the length of string.
    A string is stored inside a buffer, and may or may not fill up the entire array. The length of a string is gotten through strlen.
    As for why you get 21, it's simply called undefined behavior.
    http://cpwiki.sourceforge.net/Undefined_behaviour
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by FreeCare View Post
    What I do not understand is why when using a variable with the value of 5 to declare an array size it somehow turns into 21?
    Because you have lost the ability to read. We'll try the bold-n-caps since it helped last time:

    strlen counts the number of characters UNTIL A 0 IS REACHED WHETHER IT IS YOUR MEMORY OR NOT. The fact that you only asked for two characters (or five characters) IS COMPLETELY IRRELEVANT TO THE DISCUSSION.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Whoa, calm down.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Because you have lost the ability to read. We'll try the bold-n-caps since it helped last time:

    strlen counts the number of characters UNTIL A 0 IS REACHED WHETHER IT IS YOUR MEMORY OR NOT. The fact that you only asked for two characters (or five characters) IS COMPLETELY IRRELEVANT TO THE DISCUSSION.
    Tell you what, here is a simple tip for you. If you cannot be bothered to stay calm or refrain from being sarcastic or arsey why not...go make a cup of tea, count to 10 and move on and ignore my posts from here on. That way you wont show yourself up as a hot head

    Elysia, thank you for the link. I am reading it and trying to understand it.

    Wise saying: If someone who does not understand what they are being told it is not their fault, it is the fault of the person imparting the knowledge not conveying it in a way the respondent can understand!

    The remedy is not to get arsey

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by FreeCare View Post
    Tell you what, here is a simple tip for you. If you cannot be bothered to stay calm or refrain from being sarcastic or arsey why not...go make a cup of tea, count to 10 and move on and ignore my posts from here on. That way you wont show yourself up as a hot head

    Elysia, thank you for the link. I am reading it and trying to understand it.

    Wise saying: If someone who does not understand what they are being told it is not their fault, it is the fault of the person imparting the knowledge not conveying it in a way the respondent can understand!

    The remedy is not to get arsey
    Good. Now we both feel better, and I will make a point to ignore your posts from here on.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Elysia, just started to read you post on the use of scanf....very interesting!

    I'm working through Programming in Objective C which introduces the use of scanf.....hence my, learning of reading strings.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    Wink

    Quote Originally Posted by tabstop View Post
    Good. Now we both feel better, and I will make a point to ignore your posts from here on.
    And I yours....have a good life

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Code:
    #import <stdio.h>
    #import <string.h>
    
    int main (int argc, const char *argv[])
    {
    	int inputLength;
    
    	char userInput[10]; 
    	
    	printf("Enter a number to be reversed: ");
    	scanf("%9s", &userInput);
    	
    	inputLength = strlen(userInput);
    
    	char computerOutput[inputLength];
    
    	printf("\n\nYour entry of %s is %i characters long.\n\n", userInput, inputLength);
    
    	for (int x = inputLength-1, y = 0; x >= 0; --x, ++y)
    		computerOutput[y] = userInput[x];
    	
    	computerOutput[inputLength] = 0;
    
    	printf("computerOutput = %s\n\n", computerOutput);
    }
    BTW tabstop.....got it. Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  4. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM
  5. variable length records for strings
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 02-08-2002, 06:49 PM

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