1

I am working on a computation software written in fortran but I developed a little extension in C. The software have to be higly portable and among other constraints it have to compile on both GCC suite and Intel compiler suite. I normally test on GCC and everything was going well until I tried to compile with the intel suite. I got an "expected an expression" error on a simple for loop. I created a minimal test case and it appear that icc refuse to compile the declaration of a variable inside the for loop header.

#include <stdio.h>

int main(int argc, char** argv){
    for(int i = 0; i < 5; i++){
        printf("%d\n", i);
    }
    return 0;
}

This give me the following on intel 13, 15, 16, 17 and 18 (but not 19)

$ icc test.c -o ./test
test.c(4): error: expected an expression
    for(int i; i < 5; i++){
        ^

test.c(4): error: identifier "i" is undefined
    for(int i; i < 5; i++){
               ^

compilation aborted for test.c (code 2)

I am not a C specialist but I seen this form of for thousand of times and GCC have never been problem. Is this a GNU extension recently adopted by Intel ?

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
WIP
  • 348
  • 2
  • 11
  • 2
    Add `-std=c99` to the icc invocation – StoryTeller - Unslander Monica Feb 11 '19 at 09:10
  • 1
    Do you have to stick to a C version from past centuries or can you use some up to date compilers? Otherwise you could configure your compiler to use C99, C11 or something similar new. – Gerhardh Feb 11 '19 at 09:10
  • 1
    It probably depends on the defaults for your C compiler. The inline variable is only supported from C99 up. See this answer https://stackoverflow.com/questions/8474100/where-you-can-and-cannot-declare-new-variables-in-c – RedX Feb 11 '19 at 09:12
  • That's a C++ syntax (not plain C), at least not C90 – DDS Feb 11 '19 at 09:15
  • Indeed the test compile with -std=99 ! I work in a big project with a lot of compatibility constraints so changing compilation flags is excluded for production. The software is intended to run on supercomputers and we have to provide compatibility guaranties for old software (we still support intel 13 and gcc 5.3) but I can simply declare i outside of the for to have the compilation work as is. The question was only for information. – WIP Feb 11 '19 at 09:18
  • @WIP: icc 13 and gcc 5.3 should both support c99. icc supports it [since v12](https://software.intel.com/en-us/articles/c99-support-in-intel-c-compiler), gcc v5.3 supports both c99 and c11. – vgru Feb 11 '19 at 09:56
  • @WIP : There are no compatibility issues except in so far as this _new_ code will not compile as ISO C90. Any existing C90 code will however happily compile as C99. It depends on what you are required to "guarantee". Expecting C99 support in 2019 is not perhaps much of an ask. – Clifford Feb 11 '19 at 10:13
  • @Groo I know that c99 is supported but I cannot impose it too the rest of the project by myself. It is a big old project with huge inertia... – WIP Feb 25 '19 at 10:48
  • @Clifford I totally agree but I do not have the authority, c99 support is already discussed and probably won't be accepted in a while. The problem is that some super computers come with custom compilators that can be up to date with fortran standard and still stick to ISO C90. – WIP Feb 25 '19 at 10:48

1 Answers1

5

This is not a GCC extension, you are merely using an old version of the ICC compiler.

Loop iterator declarations inside for loops was added to the standard in the year 1999 (C99). Some older compilers may still default to the oldest C standard C90 and thereby they don't have this feature available. For example, gcc older than 5.0.0 default to "gnu90", which is C90 + extensions. Newer versions default to "gnu11", which is C11 + extensions.

On icc, gcc and clang, this can be solved by always compiling for a specific C standard:

icc -std=c11 -pedantic-errors

For older versions of the mentioned compilers, swap c11 for c99. For very new versions, you can use c17 but it doesn't seem like icc supports that yet.

For more info see What is the difference between C, C99, ANSI C and GNU C?

Lundin
  • 195,001
  • 40
  • 254
  • 396