I’m writing a C Code in which my array of length 2 char contains String My But while printing it to the Screen using puts(). I’m getting this output
My £■ 0√”
What is the reason for such codes ???
And if my array length is 2 then How can i get output of length 2+ ???
If you’ve explicitly set the length of the string to 2, you’re not leaving room for a NUL terminator, which is what
putsuses to find the end of the string. Since you don’t have one, it’ll continue printing out the contents of memory following the string you defined, until it gets to a byte in memory that happens to contain a 0.To avoid that, you generally should not specify the length when you’re creating a string literal: