tedu said:
cj said:
Can someone help me with this question? I think I've narrowed it down
to 2 out of the 4 but not sure.
char array [80] = " I am short";
Which would be true? The size of the array would be reduced to 10 or
the array size stays at 80 and the string fills array locations,
starting at array [0].
why not find out?
#include <stdio.h>
int main(int argc, char **argv) {
char array [80] = " I am short";
printf("%lu\n", (unsigned long)sizeof(array)); /* best format for
c89 */
return 0;
}
Wouldn't it be better to learn _why_ this is so, instead of just
demonstrating the final answer?
The line
char array[80] = " I am short";
reserves 80 bytes for use by the array. These can be filled in any way
imaginable. It also assigns the characters ' I am short' followed by a 0
starting with array[0]. The rest is unused, but can be if need be.
Character arrays in C are not "strings". C does not have a "string"
type. By convention (as recognised by numerous standard library
functions) "strings" are just arrays of characters terminated by a zero
character. Literal strings (the ones surrounded by double quotes) are
just convenient way to express this without actually typing the
terminating zero -- that is implied.
I'm sure there are better (more PC?) ways of saying this, but I think
it's still more helpful than just demonstrating the after-effects.
Cheers
Vladimir