Quote:
Originally Posted by mscoder
What would be the output of the following program?
|
After fixing the obvious bugs, 6.
Quote:
Originally Posted by mscoder
...it comes out as 5...i cant understand the reason..
|
Any standards-compliant compiler will output 6. If your compiler produces code that outputs 5, then it observes that
str is not used as a string anywhere, and instead treats the declaration of the array as if it was written as
char str[5] = { 83, 54, 53, 65, 66 }; . You can make sure the compiler does not do that optimization by referring to
str as a string, for example by printing it.
Quote:
Originally Posted by d3adc0de
This function counts how many characters are in the string, and outputs the result.
|
That is utterly false. First,
sizeof is not a function, it is an operator. Second, it yields the size of the type referred to, not the size allocated. When applied to a character array (which strings are in C), it yields the size of the array, not the string length.
You must be confusing
sizeof and
strlen().