Re: sizeof()'s strange behviour
- From: Philip Potter <pgp@xxxxxxxxxxxx>
- Date: Tue, 08 Apr 2008 17:14:04 +0100
Keith Thompson wrote:
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:Richard mentioned this already in his post.
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);
I'd add that C99 provides the 'z' format modifier for size_t values:
printf("sizeof s: %zd\n", sizeof s);
....although if your code isn't C99 for other reasons, this is hardly a
great reason to break C90 compatibility.
.
- Follow-Ups:
- Re: sizeof()'s strange behviour
- From: Keith Thompson
- Re: sizeof()'s strange behviour
- References:
- sizeof()'s strange behviour
- From: arnuld
- Re: sizeof()'s strange behviour
- From: Richard Heathfield
- Re: sizeof()'s strange behviour
- From: Keith Thompson
- sizeof()'s strange behviour
- Prev by Date: Re: problem with multiple source file
- Next by Date: Re: Information hiding
- Previous by thread: Re: sizeof()'s strange behviour
- Next by thread: Re: sizeof()'s strange behviour
- Index(es):
Relevant Pages
|