As per this article, the syntax is consistent with what @hvd says:
int square (int) __attribute__ ((pure));
However, it seems that gcc does not use the property not to investigate global state when compiling the following example.
#include <stdio.h> int square (int) __attribute__ ((pure)); int outerX = 7; int square(int x) { return outerX * x; } int main(){ printf("%d\n", square(5)); return 0; }
Below errors are not printed, and the code is launched and produces 35 .
gcc -Wall -Werror -pedantic -O3 Pure.c gcc --version gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Even more curious, gcc also does not care if we mutate the global state inside the function and return a different value for each call due to the change that it caused in the global state.
#include <stdio.h> int square (int) __attribute__ ((pure)); int outerX = 7; int square(int x) { outerX++; return outerX * x; } int main(){ printf("%d\n", square(5)); printf("%d\n", square(5)); printf("%d\n", square(5)); return 0; }
Output:
40 45 50
source share