
- Forum
- Programming Talk
- C and C++
- Confused with Result
Confused with Result
This is a discussion on Confused with Result within the C and C++ forums, part of the Programming Talk category; I have my C program as below: void main() { int *s,*t; s=(int*)2000; t=(int*)3000; printf("%d",(t-s)); } I thought the output ...
-
Confused with Result
I have my C program as below:
void main()
{
int *s,*t;
s=(int*)2000;
t=(int*)3000;
printf("%d",(t-s));
}
I thought the output would be 1000. But the output was 500.I was confused with why I got this output. Someone explain me on this.
-
06-06-2007, 04:39 AM #2
- Join Date
- May 2007
- Answers
- 12
t and s are pointer variables not int and they hold memory address
try using
printf("%d",(*t-*s)); instead of printf("%d",(t-s));
-
The result 500 is perfectly right. This is because t and s are declared as integer pointer variable and integer variables occupy 2 bytes generally and so the difference of 1000 space is equal to 500 integer spaces apart. This is the reason for getting output as 500.
-
06-08-2007, 06:47 PM #4
- Join Date
- Jun 2007
- Answers
- 3
because size of integer is 2 bytes.
the statement subtracts 3000-2000 in steps of 2 .
hence the answer is 2
-
Sponsored Ads
«
New Notation
|
Help to Process
»

Reply With Quote





