Windows increments variable

Discussion in 'C Programming' started by BSDBlack, Nov 2, 2011.

  1. BSDBlack

    BSDBlack Guest

    Hello,

    I wrote this little program:

    #include <stdio.h>

    int main(void) {
    int i, f[2];

    for(i = 0; i<= 2; i++) {
    scanf("%d", &f);
    fflush(stdin);
    }

    if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    &&
    f[1] == f[0])) printf("Same Numbers;\n");

    else if(f[1] < f[0] && f[2] < f[0])
    printf("%d\n", f[0]);
    else if(f[0] < f[1] && f[2] < f[1])
    printf("%d\n", f[1]);
    else if(f[0] < f[2] && f[1] < f[2])
    printf("%d\n", f[2]);

    return 0;
    }

    when I run this program under Windows and input 4, 5, 6 it returns 7;
    when I run this program under FreeBSD and input 4, 5, 6 it returns 6;

    This happens only when the biggest number is the last input.

    Why and when does Windows increment the last variable?

    I'm using 64bit Windows XP with minGW and FreeBSD 8.2
    amd64 with gcc 4.2.1

    Sebastian
     
    BSDBlack, Nov 2, 2011
    #1
    1. Advertising

  2. On Wed, 02 Nov 2011 16:03:11 -0400, BSDBlack <>
    wrote:

    > Hello,
    >
    > I wrote this little program:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > int i, f[2];
    >
    > for(i = 0; i<= 2; i++) {
    > scanf("%d", &f);
    > fflush(stdin);
    > }
    >
    > if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    > &&
    > f[1] == f[0])) printf("Same Numbers;\n");
    >
    > else if(f[1] < f[0] && f[2] < f[0])
    > printf("%d\n", f[0]);
    > else if(f[0] < f[1] && f[2] < f[1])
    > printf("%d\n", f[1]);
    > else if(f[0] < f[2] && f[1] < f[2])
    > printf("%d\n", f[2]);
    >
    > return 0;
    > }
    >
    > when I run this program under Windows and input 4, 5, 6 it returns 7;
    > when I run this program under FreeBSD and input 4, 5, 6 it returns 6;
    >
    > This happens only when the biggest number is the last input.
    >
    > Why and when does Windows increment the last variable?


    There is no f[2]. Since f is an array with two elements, the only
    elements you can assign to are f[0] and f[1]. As soon as you attempt
    to assign to f[2] (i.e. when you call scanf("%d", &f) when i == 2),
    you invoke undefined behavior, and anything at all can happen.
    In this case, a possible explanation is that your Windows compiler
    arranges variables in memory such that &i == &f[2].

    (Note also that fflush(stdin), while it may be defined to do something by
    your implementation, is undefined in standard C.)
    --
    Morris Keesan --
     
    Morris Keesan, Nov 2, 2011
    #2
    1. Advertising

  3. BSDBlack

    rudolf Guest

    In article
    <>,
    BSDBlack <> wrote:

    > Hello,
    >
    > I wrote this little program:
    >
    > #include <stdio.h>
    >
    > int main(void) {
    > int i, f[2];
    >
    > for(i = 0; i<= 2; i++) {
    > scanf("%d", &f);
    > fflush(stdin);
    > }



    f is an array with only two elements, f[0] and f[1]. The element f[2]
    is out-of-bounds, so anything can happen when you write and read to f[2].
     
    rudolf, Nov 2, 2011
    #3
  4. BSDBlack

    John Gordon Guest

    In <> BSDBlack <> writes:

    > #include <stdio.h>


    > int main(void) {
    > int i, f[2];


    > for(i = 0; i<= 2; i++) {
    > scanf("%d", &f);
    > fflush(stdin);
    > }


    As others have said, f is a two-element array. C arrays start at zero,
    so the only valid elements in f are f[0] and f[1]. If you want to have
    three elements in f, declare f as f[3].

    > if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    > &&
    > f[1] == f[0])) printf("Same Numbers;\n");


    > else if(f[1] < f[0] && f[2] < f[0])
    > printf("%d\n", f[0]);
    > else if(f[0] < f[1] && f[2] < f[1])
    > printf("%d\n", f[1]);
    > else if(f[0] < f[2] && f[1] < f[2])
    > printf("%d\n", f[2]);


    > return 0;
    > }


    > when I run this program under Windows and input 4, 5, 6 it returns 7;
    > when I run this program under FreeBSD and input 4, 5, 6 it returns 6;


    Unless things have gone horribly wrong, this program should only return
    zero.

    Did you mean that the program prints (instead of returns) 7 or 6?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Nov 2, 2011
    #4
  5. BSDBlack

    BSDBlack Guest

    On 2 Nov., 21:49, John Gordon <> wrote:
    > In <> BSDBlack <> writes:
    >
    > > #include <stdio.h>
    > > int main(void) {
    > >     int i, f[2];
    > >     for(i = 0; i<= 2; i++) {
    > >         scanf("%d", &f);
    > >         fflush(stdin);
    > >     }

    >
    > As others have said, f is a two-element array.  C arrays start at zero,
    > so the only valid elements in f are f[0] and f[1].  If you want to have
    > three elements in f, declare f as f[3].
    >
    > >     if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    > > &&
    > > f[1] == f[0])) printf("Same Numbers;\n");
    > >     else if(f[1] < f[0] && f[2] < f[0])
    > >         printf("%d\n", f[0]);
    > >     else if(f[0] < f[1] && f[2] < f[1])
    > >         printf("%d\n", f[1]);
    > >     else if(f[0] < f[2] && f[1] < f[2])
    > >         printf("%d\n", f[2]);
    > >     return 0;
    > > }
    > > when I run this program under Windows and input 4, 5, 6 it returns 7;
    > > when I run this program under FreeBSD and input 4, 5, 6 it returns 6;

    >
    > Unless things have gone horribly wrong, this program should only return
    > zero.
    >
    > Did you mean that the program prints (instead of returns) 7 or 6?
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"


    Thank you for your help, i didn't noticed that i have only 2 elements.

    and i meant print instead of return.
     
    BSDBlack, Nov 2, 2011
    #5
  6. BSDBlack

    BSDBlack Guest

    On 2 Nov., 21:49, John Gordon <> wrote:
    > In <> BSDBlack <> writes:
    >
    > > #include <stdio.h>
    > > int main(void) {
    > >     int i, f[2];
    > >     for(i = 0; i<= 2; i++) {
    > >         scanf("%d", &f);
    > >         fflush(stdin);
    > >     }

    >
    > As others have said, f is a two-element array.  C arrays start at zero,
    > so the only valid elements in f are f[0] and f[1].  If you want to have
    > three elements in f, declare f as f[3].
    >
    > >     if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    > > &&
    > > f[1] == f[0])) printf("Same Numbers;\n");
    > >     else if(f[1] < f[0] && f[2] < f[0])
    > >         printf("%d\n", f[0]);
    > >     else if(f[0] < f[1] && f[2] < f[1])
    > >         printf("%d\n", f[1]);
    > >     else if(f[0] < f[2] && f[1] < f[2])
    > >         printf("%d\n", f[2]);
    > >     return 0;
    > > }
    > > when I run this program under Windows and input 4, 5, 6 it returns 7;
    > > when I run this program under FreeBSD and input 4, 5, 6 it returns 6;

    >
    > Unless things have gone horribly wrong, this program should only return
    > zero.
    >
    > Did you mean that the program prints (instead of returns) 7 or 6?
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"


    Thank you for your help, i didn't noticed the whole time that I have
    only 2 elements.

    And i meant print instead of return, thanks for your info.
     
    BSDBlack, Nov 2, 2011
    #6
  7. On 11/2/2011 4:03 PM, BSDBlack wrote:
    > Hello,
    >
    > I wrote this little program:
    >
    > #include<stdio.h>


    While not needed, whitespace before the '<' would be nice.
    >
    > int main(void) {
    > int i, f[2];

    ^^^^
    You allocate space for two elements in the vector f (f[0] and f[1]),
    but you use f[0], f[1], and f[2]. References to f[2] are to unallocated
    space and you are unlucky that you didn't either get a compile error or
    a run-time error.

    >
    > for(i = 0; i<= 2; i++) {
    > scanf("%d",&f);
    > fflush(stdin);


    Don't do this. fflush() has no standard meaning for input streams.

    > }
    >
    > if(f[0] == f[1] || f[1] == f[2] || f[0] == f[1] || (f[1] == f[2]
    > &&
    > f[1] == f[0])) printf("Same Numbers;\n");


    The (f[1] == f[2] && f[1] == f[0]) is just typing exercise. If both
    these conditions are met, then the f[1] == f[2] test has already done
    the job.

    >
    > else if(f[1]< f[0]&& f[2]< f[0])
    > printf("%d\n", f[0]);
    > else if(f[0]< f[1]&& f[2]< f[1])
    > printf("%d\n", f[1]);
    > else if(f[0]< f[2]&& f[1]< f[2])
    > printf("%d\n", f[2]);
    >
    > return 0;
    > }
    >
    > when I run this program under Windows and input 4, 5, 6 it returns 7;
    > when I run this program under FreeBSD and input 4, 5, 6 it returns 6;


    Too bad: try turning on the higher levels of diagnostics for your
    compiler. In any case, any output at all is just random.
     
    Martin Ambuhl, Nov 3, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicolla MacPherson
    Replies:
    4
    Views:
    409
    Karl Heinz Buchegger
    Aug 13, 2003
  2. Replies:
    10
    Views:
    529
    Dennis Lee Bieber
    Jun 26, 2005
  3. 4ward.com

    Loop with float increments (frange)?

    4ward.com, Apr 14, 2006, in forum: Python
    Replies:
    3
    Views:
    469
    Tim Peters
    Apr 14, 2006
  4. M
    Replies:
    6
    Views:
    383
  5. Richard

    UDB and pointer increments and decrements

    Richard, Sep 23, 2008, in forum: C Programming
    Replies:
    10
    Views:
    488
    Tim Rentsch
    Oct 9, 2008
Loading...

Share This Page