简体   繁体   中英

why below code is giving garbage value?

why following code is giving garbage value ? here I am trying to get an string as an input from user character by character. In the following code i have got input from user and stored in string[] array then in order to do some other operations i have stored the same in other array called temp_string[i]. But surprisingly i am getting garbage value in output.and also length calculated using strlen is not correct. can anybody look at this code and explain whats going wrong?

#include<stdio.h>
#include<stdio.h>

int main()    
{
    char ch;
    int i = 0, j = 0;
    int length = 0;
    int lengthsb = 0;
    char string[100];

    printf(" Enter the string to divide\n "); 
    while(ch != '\n')
    {
        ch = getchar();
        string[i] = ch;
        i++;
    }
    char temp_string[i];
    printf("%s", string);
    i = 0;
    while(string[i] != '\n')
    {
        temp_string[i] = string[i];
        i++;
    }

    length = strlen(temp_string);
    printf("Entered string is  %s and its length is %d\n", temp_string, length);
}

You forgot to add the null at the end of the string. C strings are null-terminated, that means that all operations in c strings expect a null to mark the end of the string, including functions like strlen.

you can achieve that just adding:

string[i] = '\0';

After fill the string.

Another thing, what happens if the user enters a string bigger than 100? Is good to validate the input for these cases, otherwise you can get a buffer overflow.

You need to add a NULL - terminated at the end of your string. Add \\0 .

You need to put a '\\0' char at the end of the string so strlen() , printf() and other C functions dealing with strings will work. That is how the C API knows it reached the end of the string.

Also, you don't want to set new characters at the memory space past the string array. So you better check that in your loop (and save a last array item to set the '\\0' ).

while (ch != '\n' && i < 99)
{
  ch = getchar();
  string[i] = ch;
  i++;
}
string[i] = '\0'; // set the string terminator past the end of the input

Remember to do the same after copying the characters to temp_string . (By the way, you can replace that loop with a call to strcpy() , that does exactly that, except it will end only when it finds a '\\0' .)

You might also want to read What's the rationale for null terminated strings?

Here is your Final Code:

#include<stdio.h>
#include<stdio.h>
#include<string.h>

int main()
{ 
char ch;
int i = 0, j = 0;
int length = 0;
int lengthsb = 0;
char string[100];
char temp_string[100];

printf(" Enter the string to divide\n ");
while(ch != '\n')
{
ch = getchar();
string[i] = ch;
i++;
}
string[i]=NULL;

printf("%s", string);
i = 0;
while(string[i] != '\0')
{
temp_string[i] = string[i];
i++;
}
temp_string[i]=NULL;

length = strlen(temp_string);
printf("Entered string is  %s and its length is %d\n", temp_string, length);
}

In the above code what exactly you are missing is NULL or '\\0' termination of the string. I just added it to make it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM