[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

odd compiler behaviour?



I can't see why the second program fails to compile, as far as I would expect these programs are identical.
Does anyone knows what goes wrong?

cheers,
floris

~$ gcc --version
2.95.4
~$ cat << EOF > prog1.c
#include <stdio.h>

int main()
{
  char* record;
  int record_size = 10;
  int letter = 'a';
  int i;
  record = (char*) malloc (record_size);

  for (i=0; i < record_size; i++)
    {
      record[i] = (char) letter++;
      printf ("record[%d] = %c\n", i, record[i]);
    }
  return 0;
}
EOF
~$ gcc prog1.c
~$ cat << EOF > prog2.c
#include <stdio.h>

int main()
{
  char* record;
  int record_size = 10;
  record = (char*) malloc (record_size);

  int letter = 'a';
  int i;
  for (i=0; i < record_size; i++)
    {
      record[i] = (char) letter++;
      printf ("record[%d] = %c\n", i, record[i]);
    }
  return 0;
}
EOF
~$ gcc prog2.c
prog2.c: In function `main':
prog2.c:9: parse error before `int'
prog2.c:11: `i' undeclared (first use in this function)
prog2.c:11: (Each undeclared identifier is reported only once
prog2.c:11: for each function it appears in.)
prog2.c:13: `letter' undeclared (first use in this function)
~$

~$ cat



Reply to: