sizeof

S

shaanxxx

I have following programme:

#include<stdio.h>
int main()
{
int i=1;

char *ptr=(char *)&i;

if (*ptr)
{
printf("Little Indian\n");
}
else
{
printf("Big Indian\n");
}
printf("%d %d %d\n",sizeof(void
*),sizeof(sizeof(char)),sizeof(char));
return 0;
}

I used %d to print values returned from sizeof. I am wondering why
this programme gives right result on 64bit machines. Somewhere on this
group , i have seen suggestion that %d should not be used. Thats why i
wanted to test %d with sizeof on big Indian 64bit where it should
fail(as per my understanding, And understanding can go wrong).

any comment on this would be appreciated.

I replaced %d with %c , second printf doesnt print any thing.

$ cc -xarch=v9 print.c -o print
$ ./print
Big Indian
8 8 1
$ uname -a
SunOS sunflare 5.8 Generic_108528-18 sun4u sparc SUNW,Ultra-4
13:02:04-odali@sunflare


redhats$ make print
cc print.c -o print
redhats$ ./print
Little Indian
8 8 1
redhats $ uname -a
Linux redhats.xxx.com 2.6.9-34.ELsmp #1 SMP Fri Feb 24 16:56:28 EST
2006 x86_64 x86_64 x86_64 GNU/Linux
 
E

Eric Sosman

shaanxxx said:
I have following programme:

#include<stdio.h>
int main()
{
int i=1;

char *ptr=(char *)&i;

if (*ptr)
{
printf("Little Indian\n");
}
else
{
printf("Big Indian\n");
}
printf("%d %d %d\n",sizeof(void
*),sizeof(sizeof(char)),sizeof(char));
return 0;
}

I used %d to print values returned from sizeof. I am wondering why
this programme gives right result on 64bit machines. Somewhere on this
group , i have seen suggestion that %d should not be used. Thats why i
wanted to test %d with sizeof on big Indian 64bit where it should
fail(as per my understanding, And understanding can go wrong).

When the conversion specifier ("%d") and the corresponding
argument (a size_t) do not match, the behavior is undefined.
"Undefined" does not mean "The program will misbehave in some
specified way" -- if the misbehavior were specified, then it
wouldn't be "undefined!"

Sometimes, undefined behavior will turn out to be the same
as the behavior you hoped for. Sometimes it will be just a
little bit different from what you hoped for, but perhaps so
slightly that you don't notice it at first. Sometimes, the
program will crash. In principle, anything at all can happen.
any comment on this would be appreciated.

I replaced %d with %c , second printf doesnt print any thing.

More undefined behavior. Anything can happen, and the
question of why "this" happened instead of "that" cannot be
answered in terms of the C language.

Weak analogy: You approach someone in the street, and say
to him "Metronomes in purple election jump deliciously wet,"
a grammatically correct but nonsensical English sentence. Does
the person ignore you, run away, dial 9-1-1, punch you in the
nose, or give you a lump of gold the size of your mother-in-law?
The rules of English do not dictate any particular behavior, and
anything might happen. It is not very useful to try to draw
conclusions about the "meaning" of the sentence from the behavior
of the startled person.
 
S

santosh

shaanxxx said:
I have following programme:

#include<stdio.h>
int main()
{
int i=1;

char *ptr=(char *)&i;

if (*ptr)
{
printf("Little Indian\n");
}
else
{
printf("Big Indian\n");
s/Indian/Endian

}
printf("%d %d %d\n",sizeof(void *),sizeof(sizeof(char)),sizeof(char));

What do you expect to get from sizeof(sizeof(char))? The inner sizeof
will yield a value of type size_t which will be evaluated by the outer
sizeof. Is this what you wanted.
return 0;
}

I used %d to print values returned from sizeof. I am wondering why
this programme gives right result on 64bit machines. Somewhere on this
group , i have seen suggestion that %d should not be used. Thats why i
wanted to test %d with sizeof on big Indian 64bit where it should
fail(as per my understanding, And understanding can go wrong).

any comment on this would be appreciated.

It probably works only by chance. The type of the value returned by
sizeof is size_t which is defined as an unsigned type. Exactly what
unsigned type it is, is upto the implementation. It could be unsigned
int, unsigned long etc.

Using d with an unsigned value is asking for trouble. It happens to
work in this case since the value returned are small and probably both
signed and unsigned ints are the same size on your platform but this
is by no means valid anywhere else.

If you have only a C89 implementation, use an lu format specifier and
explicitly cast the return value of sizeof to unsigned long. If your
implementation supports C99, then you can use the zu format specifier
explicitly meant for size_t values.

<snip machine details>
 
B

Beej Jorgensen

When the conversion specifier ("%d") and the corresponding
argument (a size_t) do not match, the behavior is undefined.

I believe "%zu" is what he's after for printing size_t.

-Beej
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top