NO

Author Topic: error... syntax? runtime? compiler bug?  (Read 3740 times)

GooseEgg

  • Guest
error... syntax? runtime? compiler bug?
« on: April 24, 2013, 08:52:08 AM »
hi there. I'd wanted to know if this is a programming error or a compiler error?

This code compiled as a win32 console application. When ID_num is allowed to increment, it reaches 53, then the program freezes and closes itself.

When I compile this with 'Quincy 2005' IDE (LCC compiler), it does not crash at all. it works as I expected, and can reach 99 (C_LIM).


Code: [Select]
#include <stdio.h>
#define COUNT 100
#define C_LIM (COUNT - 1)


int main(void) {
int value_collector[COUNT],
ID_num = 0;


printf("\nEnter integer, and finalize input by entering '-1'\n\n");
printf("%2d Enter an Integer:",(ID_num + 1));
scanf("%d", &value_collector[ID_num]);

while(ID_num < C_LIM && value_collector[ID_num] != -1) {
ID_num++;
printf("%2d Enter an Integer:", (ID_num + 1));
scanf("%d", &value_collector[ID_num]);
}

return 0;
}

I am a beginner and probably overlooking something. thanks.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: error... syntax? runtime? compiler bug?
« Reply #1 on: April 24, 2013, 09:04:42 AM »
&& value_collector[ID_num] != -1

You have defined a local integer array. In contrast to many other programming languages, C does not initialise local variables, which means the array contains garbage. "-1" is "frequently found garbage" ;-)

GooseEgg

  • Guest
Re: error... syntax? runtime? compiler bug?
« Reply #2 on: April 24, 2013, 09:13:45 AM »
ok so I added the initialization immediately following ID_num = 0;

value_collector[ID_num] = 0;

same problem still, though now it reaches ID_num of 57 before crashing out...


thanks though!

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: error... syntax? runtime? compiler bug?
« Reply #3 on: April 24, 2013, 10:51:12 AM »
value_collector[ID_num] = 0;

That sets element zero to zero, but not the rest of the array. This is better:

   while(ID_num < C_LIM) {
      ID_num++;
      printf("%2d Enter an Integer:", (ID_num + 1));
      scanf("%d", &value_collector[ID_num]);
      if (value_collector[ID_num] == -1) break;
   }

Offline DMac

  • Member
  • *
  • Posts: 272
Re: error... syntax? runtime? compiler bug?
« Reply #4 on: April 24, 2013, 06:05:57 PM »
To initialize a locally declared array use memset.

Code: [Select]
int value_collector[COUNT];
memset(&value_collector, 0, sizeof(value_collector));
No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: error... syntax? runtime? compiler bug?
« Reply #5 on: April 24, 2013, 09:06:59 PM »
Maybe it's time to think again your program's code.
Code: [Select]
#include <stdio.h>
#define COUNT 100
#define C_LIM COUNT

int main(void)
{
int value_collector[COUNT], ID_num = 0;

printf("\nEnter integer, and finalize input by entering '-1'\n\n");

do
{
printf("%2d Enter an Integer:", (ID_num + 1));
scanf("%d", &value_collector[ID_num]);
if (value_collector[ID_num] == -1) break;
ID_num++;
} while (ID_num < C_LIM);

return 0;
}
May the source be with you

GooseEgg

  • Guest
Re: error... syntax? runtime? compiler bug?
« Reply #6 on: April 24, 2013, 10:49:56 PM »
@timovjl, heh I tried your modified code. same result. It crashes as well when it gets to "57 Enter an Integer".

@jj2007, replaced my loop with your modified loop code. It too crashes at the scanf, this time at "56 Enter an Integer".

Each array element is initialized by its assignment via 'scanf' as far as I can tell, and it doesn't near the end of the array before crashing.

To test the code, all I am entering: 1
on each line. Anyone else try to run this as a program until it gets to 99?



Offline jj2007

  • Member
  • *
  • Posts: 536
Re: error... syntax? runtime? compiler bug?
« Reply #7 on: April 25, 2013, 12:21:17 AM »
Hi GooseEgg,
The suggestions of timovjl and myself are equivalent. Your problem is elsewhere. Have you tried running the exe from a DOS prompt?

Here is one more suggestion. It runs just fine...

Code: [Select]
#include <stdio.h>
#define COUNT 100

int main(void)
{
int value_collector[COUNT], ID_num = -1;
printf("\nEnter integer, and finalize input by entering '-123'\n\n");

do
{
ID_num++;
printf("%2d Enter an Integer:", (ID_num + 1));
scanf("%d", &value_collector[ID_num]);
} while (ID_num < COUNT-1 && value_collector[ID_num] != -123);

return 0;
}

GooseEgg

  • Guest
Re: error... syntax? runtime? compiler bug?
« Reply #8 on: April 25, 2013, 06:56:35 AM »
Well thanks for all the input from all of you. I guess the problem I am having is mine alone, because only when I run the executable from pelles c does it occurs.

Running it from 'cmd' prompt gives me no problem what-so-ever... Strange.

Now I imagine all the solutions you have all provided work fine or better than my presented algorithm, so at least something was gained from this.

So to summarize: All the code is fine and it runs fine as executable from command prompt. Only running the executable from Pelles C do I get a problem, and I seem to be alone on that.  8)