Doubt in character array

Hello, If I have a character array

char ch [ ] = { ’ h ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ };

I have not used any null character here. It is now printing hello with some garbage values.

Now when I print the size of array why it is not including the garbage values while printing the size. I mean size should be

( characters present in it + garbage characters)

Here is the example code :

Oh… this is the code not the above one :

https://ide.codingblocks.com/s/365604

hello @yashsharma4304

size of array will always be the number of entries it have ( it get decided on compile time only for static allocation).

that garbage related issue happens while printing.
becuase while printing compiler will look for \0 to stop.
so it will keep printing until it found \0 . hence it gives garbage result if we dont add \0 in the end

1 Like

Ok :slightly_smiling_face: :slightly_smiling_face: