Windows increments variable

B

BSDBlack

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
 
M

Morris Keesan

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

rudolf

BSDBlack said:
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].
 
J

John Gordon

In said:
#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?
 
B

BSDBlack

In said:
#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?


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

and i meant print instead of return.
 
B

BSDBlack

In said:
#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?


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

Martin Ambuhl

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top