6

When I compile the following program I get errors :

gcc tester.c -o tester

tester.c: In function ‘main’:
tester.c:7:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_X’
tester.c:7:17: error: ‘ptr_X’ undeclared (first use in this function)
tester.c:7:17: note: each undeclared identifier is reported only once for each function it appears in
tester.c:10:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptr_Y’
tester.c:10:17: error: ‘ptr_Y’ undeclared (first use in this function)

#include <stdio.h>

int main() {
  int x = 10;
  int y = 20;

  int *restrict ptr_X;
  ptr_X = &x;

  int *restrict ptr_Y;
  ptr_Y = &y;

  printf("%d\n",*ptr_X);

  printf("%d\n",*ptr_Y);
}

Why am I getting these errors ?

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

5

Not all compilers are compliant with the C99 standard. For example Microsoft's compiler, does not support the C99 standard at all. If you are using MSVC on a x86 platform you will not have access to this critical optimization option.

When using GCC, remember to enable the C99 standard by adding -std=c99 to your compilation flags. In code that cannot be compiled with C99, use either __restrict or __restrict__ to enable the keyword as a GCC extension.

From here.

sje397
  • 41,293
  • 8
  • 87
  • 103
1

Restrict is part of C99, and therefore you have to compile it as a C99 program by specifying -std=c99 flag to gcc.

gcc -std=c99 tester.c -o tester
halex
  • 16,253
  • 5
  • 58
  • 67
  • 3
    why do I need to specify c99 as a flag to the compiler ? c99 was introduced in 1999 and now it is 2012. – saplingPro Oct 31 '12 at 09:51
  • @grassPro See http://stackoverflow.com/questions/5060799/c99-not-default-c-version-for-gcc – halex Oct 31 '12 at 09:52
  • @grassPro The default setting for gcc is: compile as non-standard crap. Always use -std=c99, -std=c89 or soon, -std=c11. – Lundin Oct 31 '12 at 10:40