Rupesh Kumar said:
If I typecast a byte to a long integer in different endian (Big &
Little endian) systems does it behave differently.
No. But first, some answers to questions you didn't ask.
The term is "cast", not "typecast". A cast is an operator, consisting
of a type name in parentheses, that specifies a conversion. And in
most cases, whatever conversion you want will be performed implicitly;
a cast is unnecessary and, in some cases, dangerous.
For example:
unsigned char one_byte = 42;
long foo = one_byte; /* implicit conversion from unsigned char to long */
The value of foo will be 42. The representation of either char or
long is irrelevant. Conversions are defined to operate on values, not
on representations.
I am confused between the following.
If the address of the byte is 0 then seeing the address as long should
be different in big and little endian systems.
or
As the language is portable,long will have the same value when you see
it in little or big endian systems
Here, and in your followup, you talk about addresses, which makes me
wonder whether you're really asking about integer conversions (say,
from unsigned char to long) or about pointer conversions. Pointer
conversions can be used to implement what's called "type punning",
whereby an object of one type is treated as if it were of another
type. This does depend on the representation, and it can be dangerous
(and it's usually unnecessary). For example:
unsigned char one_byte = 42;
unsigned char *byte_addr = &one_byte;
long *long_addr = (long*)byte_addr; /* dangerous */
long foo = *long_addr; /* dangerous */
Here the code is looking at the object one_byte *as if* it were of
type long. Since it isn't, this can go bad in a number of ways. The
address of one_byte might not have the right alignment for type long,
and even if it's aligned correctly you're (almost certainly) looking
at bytes that aren't part of the object.
What are you actually trying to do?