#strings in C

1 messages · Page 1 of 1 (latest)

dapper belfry
#

i set the string length to 5 and tried to store 5 characters in it, but why does it say the length is 7 in output?

olive crescentBOT
#

<@&987246683568103514> please have a look, thanks.

olive crescentBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

dapper belfry
#

also the last 2 letters are garbage values? since they are random every time i run the program
'u' and 'j' here

smoky brook
#

u have undefined behavior here

#

ur strings are incorrect

#

in c, arrays dont know their length

#

so how does strlen work? how does it know where the text ends?

#

it goes until it finds the magic value \0

#

the null-terminator

#

in ur program, it seems it found one 2 spots after the string

#

so it printed up to there

#

but it could be anywhere or nowhere. its undefined behavior and pure coincidence that there was a \0 in memory there

#

sooo... how to do it proper? ur array has to have 6 spots, not 5. and the last char should be '\0'

#

then it works

bleak basin
scenic vortex
#

better way is char str[] = "apple", let compiler do it

#
#include <stdio.h>
#include <string.h>
int main(){
  char str1[6] = "apple";
  printf("%s \n",str1);
  printf("Length of the sting is %d \n",strlen(str1));
  for(int i = 0; str1[i] != 0; i++)
    printf("%c \n",str1[i]);
  return 0;
}
olive crescentBOT
dapper belfry
#

oh i guess then i cant use the strlen

smoky brook
#

any of the string methods will assume the end is marked by the null terminator

dapper belfry
smoky brook
#

some of the methods also have an overload where u can explicitly tell it the length manually, so that it can also work with strings without null terminator

dapper belfry
#

oh its weird behaviour cuz strlen() was expecting a string?

smoky brook
#

pure coincident

#

remember that c has no safety features here. it just reads the memory beyond ur array

dapper belfry
#

i ran it again

#

how is it still 7?

#

i ran it a few times its always 7?

#

maybe the "0" byte is still at that memory location until i over write it?

smoky brook
#

it could be sth else in ur code sitting there

#

in any case, it's undefined behavior

#

and UB doesn't necessarily mean that it's random

dapper belfry
#

oh

#

noted , thanks!

#

also why does it says 'unmatched' is not initialized?

smoky brook
#

mind sharing the code as formatted text, not image

#

then we can try ourselves

#

btw, u should avoid that comma thing

#

int end, count = 0;

#

not even sure it works the way u want

#

wouldn't be surprised if end is not initialized here