Many string functions seem to work without the null byte... is it necessary to add it all the time?
and my second question, does scanf %s automatically ads the null byte?
Many string functions seem to work without the null byte... is it necessary to add it all the time?
and my second question, does scanf %s automatically ads the null byte?
Many string functions seem to work without the null byte... is it necessary to add it all the time?
Which functions are you talking about specifically? If the function takes a size as an argument or only reads up to a certain delimiter in the string that's guaranteed to be in the string, you won't need a 0-byte. Otherwise you will. Generally all functions that you'd usually refer to as "string functions" (that is the ones in string.h) do expect the string to be nullterminated.
Note that since the byte that comes after a string in memory could very well be 0, it's perfectly possible that you might still get the correct result if you pass a string without a 0-terminator to a function that expects a 0-terminated string. However that's undefined behavior and will break as soon as the next byte in memory happens to be something else.
and my second question, does scanf %s automatically ads the null byte?
Yes.
is it necessary to add it all the time?
Yes, because that's the definition of a string. Just because something seems to work at the moment you test it doesn't mean it's correct or guaranteed to work all the time. C is one of those languages that won't help you if you do something stupid, so it's up to you to avoid doing stupid things.
if i have an empty char array and I pass it to scanf
will scanf add the nullbye?
if i have an empty char array and I pass it to scanf
will scanf add the nullbye?
You already asked this question and it was already answered: yes.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.