Exforsys
+ Reply to Thread
Results 1 to 4 of 4

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 ...

  1. #1
    rachelle is offline Member Array
    Join Date
    Apr 2006
    Answers
    97

    Exclamation 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.


  2. #2
    Mukhtar Ahmad is offline Junior Member Array
    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));


  3. #3
    Adrian is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    124
    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.


  4. #4
    tarungosain20 is offline Junior Member Array
    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



Latest Article

Network Security Risk Assessment and Measurement

Read More...