Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Windows increments variable

Reply
Thread Tools

Windows increments variable

 
 
BSDBlack
Guest
Posts: n/a
 
      11-02-2011
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[i]);
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
 
Reply With Quote
 
 
 
 
Morris Keesan
Guest
Posts: n/a
 
      11-02-2011
On Wed, 02 Nov 2011 16:03:11 -0400, BSDBlack <(E-Mail Removed)>
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[i]);
> 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[i]) 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 -- http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
rudolf
Guest
Posts: n/a
 
      11-02-2011
In article
<(E-Mail Removed)>,
BSDBlack <(E-Mail Removed)> 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[i]);
> 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].
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      11-02-2011
In <(E-Mail Removed)> BSDBlack <(E-Mail Removed)> writes:

> #include <stdio.h>


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


> for(i = 0; i<= 2; i++) {
> scanf("%d", &f[i]);
> 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
BSDBlack
Guest
Posts: n/a
 
      11-02-2011
On 2 Nov., 21:49, John Gordon <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)> BSDBlack <(E-Mail Removed)> writes:
>
> > #include <stdio.h>
> > int main(void) {
> > * * int i, f[2];
> > * * for(i = 0; i<= 2; i++) {
> > * * * * scanf("%d", &f[i]);
> > * * * * 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
> (E-Mail Removed) * * * * * * *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.
 
Reply With Quote
 
BSDBlack
Guest
Posts: n/a
 
      11-02-2011
On 2 Nov., 21:49, John Gordon <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)> BSDBlack <(E-Mail Removed)> writes:
>
> > #include <stdio.h>
> > int main(void) {
> > * * int i, f[2];
> > * * for(i = 0; i<= 2; i++) {
> > * * * * scanf("%d", &f[i]);
> > * * * * 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
> (E-Mail Removed) * * * * * * *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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-03-2011
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[i]);
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDB and pointer increments and decrements Richard C Programming 10 10-09-2008 08:21 AM
Possible to control increments of font re-sizing? M HTML 6 07-27-2007 12:02 AM
Loop with float increments (frange)? forum@anton.e4ward.com Python 3 04-14-2006 03:34 PM
How does one write a function that increments a number? anonymousnerd@gmail.com Python 10 06-25-2005 11:06 PM
Rows: display a pattern that increments in each row Nicolla MacPherson C++ 4 08-13-2003 09:02 AM



Advertisments