Hi,guys,
char s1[10] = "abcde";
char s2[3];
printf("%d",s1-s2);
Comparing pointers that are not part of the same object or array, or
one past the end of the same object or array, produces undefined
behavior.
What should be the result on a 32-bit machine?
There is no 'should', your program produces undefined behavior so the
C standard no longer places any requirements at all on what happens.
It is 8 on my machine,
why?
Since the behavior is undefined, there is no 'why' as far as C is
concerned.
Here is the output of "uname -a" on my machine:
$uname -a
SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250
What is 'uname' and why do we care what it outputs?
If you are questioning the alignment your compiler uses in defining
data objects, and there is no guarantee that the output of your code
has anything at all to do with that, then you are asking in the wrong
place.
Data alignment in C is not defined by the language, it is specifically
implementation-defined. That means it is up to the compiler provider
to do whatever it is they want to do. It may be based on the
architecture of the underlying hardware, it may be based on the kind
of car the head programmer drives, it may be based on the fact that
the compiler writer has stock in a memory chip company, and wants to
make sure that you need to buy lots of extra memory.
If you want to know what alignment your compiler uses and why, either
contact their technical support or ask in a group that supports your
compiler. It is not a language issue.