#include<stdio.h>

main()

{
int a=258;
int *num=&a;
printf("%d %d",*((char *)num),*((char *)num+1));


}

does the output depends on little endian or big endian structure of machine on which i am running this ? thanks.

Recommended Answers

All 5 Replies

Yes, I think the output will be different on *nix then it is on MS-Windows. Have you tested it on both systems?

Yes it does matter, however most *nix systems these days are running x86 processors so should have the same results, assuming that they have the same size word. That said, most *nix systems are 64-bits any longer, so you might want to specify a long long int (64-bits) instead of a simple int (32-bits).

no , i haven't tested on both types of systems. but will the ouptput differ if i run this code on different machines ? that mean i will get highest bits firstly on big-endian and lowest bits firstly on little endian ? right ?

The program, as written, will invoke undefined behavior and most likely not depend on endianness at all.

num+1 produces a pointer that points to the address that num points to plus sizeof(int). You then cast that pointer to char* and dereference it. Since num+1 points outside of the bounds of the object that num points to, this is undefined behavior. In practical terms this will most likely give you the byte that is stored in memory after a. What that byte is will not really depend on endianness.

I assume what you meant to do is *(((char *)num)+1). That is cast num to char* first and then add 1 to it, producing a pointer to the second byte of a.

yes! sorry for that. yes i am intending to do that only which you have wriiten in last line. hete it goes :

#include<stdio.h>
main()
{
int a=258;
int *num=&a;
printf("%d %d",*((char *)num),*(((char *)num)+1));
}

now, can you answer my questions which was asked ? thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.