-3

I've made a simple C program and I try to compile it using gcc compiler. But when I execute gcc test.c -o test it throws some errors:

test.c:1:19: error: cstdlib: No such file or directory
test.c:2:20: error: iostream: No such file or directory
test.c: In function ‘main’:
test.c:8: error: ‘for’ loop initial declaration used outside C99 mode

And my C program is very simple so I don't think the problem is in the code:

#include <cstdlib>
#include <iostream>

int main(int args, char **argv){
    int result[500];

    for (int i = 0; i < sizeof(result); ++i){
        result[i] = 1;
    }

    return 0;
}

Thanks!

Marc Ortiz
  • 2,242
  • 5
  • 27
  • 46
  • 4
    you need using c++ header files use g++. And for your this code you don't need any header file. – Grijesh Chauhan Aug 20 '13 at 10:50
  • 3
    You either code in C++ (then need to use `g++` and not `gcc`) or in C (then get rid of the C++ includes). – Nbr44 Aug 20 '13 at 10:51
  • 1
    To clarify the above, change the includes to "stdlib.h" and "stdio.h" – Inisheer Aug 20 '13 at 10:53
  • 7
    Another thing: **sizeof** gives the size in chars, not the number of places in the array, so you need to write **i < sizeof(result) / sizeof(result[0])**. – Thomas Padron-McCarthy Aug 20 '13 at 10:54
  • You have not written a C program, you've written a C++ program. Either change the compiler front end from gcc to g++ or convert the code to proper C. – mah Aug 20 '13 at 10:55
  • @ThomasPadron-McCarthy and the last error related with the for loop? – Marc Ortiz Aug 20 '13 at 10:56
  • That is not compiler error but a bug in your code Read: [Weird behavior when printing array in C?](http://stackoverflow.com/questions/18009725/weird-behavior-when-printing-array-in-c/18009736#18009736) to understand @ThomasPadron-McCarthy – Grijesh Chauhan Aug 20 '13 at 10:58
  • 2
    If you are compiling with gcc, add **-std=c99**. Defining variables in the for loop header was new in C99. – Thomas Padron-McCarthy Aug 20 '13 at 10:58

2 Answers2

7

The .c extension means that GCC is interpreting the source files as C code. Your header includes are C++ headers though, and so the C compiler gives you those first two errors. To fix this you can either:

  • Change the file extensions to .cpp instead of .c, which will cause your source code to be interpreted as C++ code by GCC. In C++ what you have written is all valid.

Or:

  • Change the first header include to #include <stdlib.h> and remove #include <iostream>, as there is no exact equivalent of iostream in C. You don't actually need either of those headers for your program though, because the program is very simple.

As for the for loop error, the problem is that you've declared the variable i within the initialization step of the for loop. This isn't allowed prior to C99, and so you can fix this by moving the declaration of i to above the loop.

So the working program might look like this:

#include <stdlib.h>
#include <stdio.h>

int main(int args, char **argv){
    int result[500];
    int i;

    for (i = 0; i < sizeof(result)/sizeof(result[0]); ++i){
        result[i] = 1;
    }

    return 0;
}

In this case I left the header includes in the program, even though they aren't needed. stdio.h is the closest to iostream you can get in C.

As pointed out by others, for proper behaviour the for loop condition should also be changed to something like i < sizeof(result)/sizeof(result[0]).

This is because sizeof, when used with an array, gives the size of the array in bytes, which depending on your platform will most likely mean you get the wrong number of iterations in your loop. (An int is typically going to be four bytes, and so the way you've written it your loop would iterate 2000 times instead of 500. By dividing through by the size of one element of your array, you get the right result every time.)

An even better approach might be to #define the magic number of 500 at the start of your program, and refer to it that way whenever it is needed.

Overall the main problem is that what you really have is a C++ program, but you're wanting it to be a C program. You'll have to make up your mind as to which language you really want to use.

Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33
  • And the last error related with the for loop? Do you know that error? – Marc Ortiz Aug 20 '13 at 10:56
  • The last error is also because you are using C++ syntax with a C program. If you switch to C you must declare the loop variable outside the loop. For C++ it's ok. – Devolus Aug 20 '13 at 11:00
  • Also suggest correction `sizeof(result) / sizeof(result[0])` as loop condition: read [this answer](http://stackoverflow.com/questions/18009725/weird-behavior-when-printing-array-in-c?lq=1) – Grijesh Chauhan Aug 20 '13 at 11:04
  • @Devolus: Defining loop variables inside the loop has worked in C since the 1999 standard, so you don't need to switch to C++ for that. Well, unless you are using Visual Studio, which is still stuck in 1989. – Thomas Padron-McCarthy Aug 20 '13 at 11:05
  • 1
    Thanks, I've made the suggested changes (or am currently editing them in) – Adam Goodwin Aug 20 '13 at 11:10
  • @ThomasPadron-McCarthy, the error message says otherwise, so the solution is either to use the c99 switch, which was provided already in some comment, or define the loop variable outside. ;) – Devolus Aug 20 '13 at 12:18
  • Note that with these corrections, your program will *compile* without errors and even *run* without errors (when invoked on the command line). However, you will not *see* any result; you can inspect the `result` array only using a debugger. – Jongware Aug 20 '13 at 13:52
1

cstdlib and iostream are C++ header files. For cstdlib, you might use stdlib.h instead. However, you might not use iostream since iostream is a pure C++ I/O lib. I guess what u want might be "stdio.h"

What's more, if you want to use the feature like declare the variable "i" in the initial part of the "for" loop, you might compile your program in C99 mode on account of that the C language standard before C99 does not support this kind of usage.

To compile a c source code, such as "a.c", in C99 mode, just add -std=c99 parameter to your gcc command. e.g.: "gcc -std=c99 a.c"

House.Lee
  • 241
  • 1
  • 3
  • 12