Re: sizeof()'s strange behviour



Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:
arnuld said:
[...]
printf("sizeof(%s): %d\n", s, sizeof(s));

...so we'd expect this to print 16 in place of the second % format
specifier. Note that your printf is misleading. It will print:

sizeof(Saurabh Nirkhey): 16

but in fact it's sizeof s that is 16.
[...]

Furthermore, "%d" causes printf to expect an int argument, but
sizeof(s) is of type size_t. It's likely to happen to work on systems
where int and size_t are the same size, but the right way is (with a
couple of other tweaks):

printf("sizeof s: %d\n", (int)sizeof s);

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: sizeof()s strange behviour
    ... Note that your printf is misleading. ... but in fact it's sizeof s that is 16. ... Furthermore, "%d" causes printf to expect an int argument, but ... but in the context of returning the result from the OP's ...
    (comp.lang.c)
  • Re: Size of extern int x;
    ... One may interpret the OP's question as: "What is sizeof x?" ... int main ...
    (comp.lang.c)
  • Re: RunTime Datatype Determination in C
    ... int main ... ERROR: printf without a prototype. ... sizeof returns size_t, not int. ...
    (comp.lang.c)
  • Re: RunTime Datatype Determination in C
    ... int main ... ERROR: printf without a prototype. ... sizeof returns size_t, not int. ...
    (comp.lang.c)
  • sizeof()s strange behviour
    ... sizeof() operator gives 2 different types of size outputs:\ but I do not ... int my_size(char s) ... sizeof(Saurabh Nirkhey): 4 ...
    (comp.lang.c)