Author Topic: Problem compiling "for loop" with codeblocks compiler.  (Read 21083 times)

Offline deva

  • Single posting newcomer
  • *
  • Posts: 3
Problem compiling "for loop" with codeblocks compiler.
« on: November 09, 2011, 10:54:46 pm »
Hi friends,


I am using GNU GCC Compiler

OS: win7 32bit


/* Program 4.2 Drawing a box */
#include <stdio.h>
int main(void)
{
printf("\n**************"); /* Draw the top of the box */
for(int count = 1 ; count <= 8 ; ++count)
printf("\n* *"); /* Draw the sides of the box */
printf("\n**************\n"); /* Draw the bottom of the box */
return 0;

}


when i try to compile the code i got fallowing error.

C\48.c|6|error: 'for' loop initial declarations are only allowed in C99 mode|
C\48.c|6|note: use option -std=c99 or -std=gnu99 to compile your code|


If anybody know how to change it to c99mode please help me.


Thanks & Regards,

Deva



Hi, I changed to -std=c99 mode is this way correct ?

Please see picture
Untitled.png:








Hi friends,

My problem solved. many of them adviced me to change settings to -std=c99 But its not worked because my compiler is GNU GCC Compiler.

Here is see how the problem is fixed.

In the Compiler and Debugger settings. Compiler settings tab - Other Options. I added there -std=gnu99


So Problem solved.


Friends thanks very much for your advices & time.


Thanks & Regards,

Deva







« Last Edit: November 10, 2011, 04:24:57 pm by deva »

zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #1 on: November 09, 2011, 11:12:10 pm »
Go to the Project menu, then Build Options..., then Compiler Settings|Other options tab, then enter -std=c99 and hit OK.

Offline deva

  • Single posting newcomer
  • *
  • Posts: 3
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #2 on: November 10, 2011, 07:40:27 am »

Hi,

Thanks for your reply. In the projecet menu build options is disable I mean its not highlited. How can we enable it?.


zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #3 on: November 10, 2011, 09:31:30 am »
CB is project based. Before you write any code, create a project from Create New Project on the CB startup screen, and select Console Application - the Build Options will be enabled after you create a project.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12090
    • Travis build status
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #4 on: November 10, 2011, 10:21:23 am »
CB is project based. Before you write any code, create a project from Create New Project on the CB startup screen, and select Console Application - the Build Options will be enabled after you create a project.
Wrong, C::B supports single file compilation, too.
For the compilation it uses the default compiler,
so you can set the option in the settings,
but this is not a good practice in the long run.
No, debugging is supported in single file mode.

(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #5 on: November 10, 2011, 10:32:38 am »
> Wrong, C::B supports single file compilation, too.

What is "wrong"? Do you deny that that CB is project based? Can you use Build Options without a project, which is what the OP needs to do, without using what you yourself say is "bad practice"? No, you can't.
« Last Edit: November 10, 2011, 10:34:52 am by Neil Butterworth »

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7265
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #6 on: November 10, 2011, 10:46:30 am »
I gues you use a *.c file.
The easiest way would be to rename it to *.cpp, so it is compiled with the c++-compiler.

zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #7 on: November 10, 2011, 10:50:02 am »
> The easiest way would be to rename it to *.cpp, so it is compiled with the c++-compiler.

But then he runs into problems if he uses C99 features not supported by C++.

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #8 on: November 10, 2011, 11:44:11 am »
IMO, Jens is correct. You problem consists in "for( int count = 1 ; ..." This kind of declaring local variables in a loop statement (or other statements) is usual in C++ for a long time. Perhaps, a norm is it starting from C99. GCC is over strict sometimes ...

(1) Either switch to C++ by renaming 48.c to 48.cpp or selecting C++ compiler explicitely.
(2) Or declare "int count" like a normal variable declaration at the beginning of main().

When you opt for (1) you can get a similar paranoid message. When you opt for (2), you should pass even with a  C compiler.

zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #9 on: November 10, 2011, 12:22:23 pm »
@Radek. It is perfectly legal (and good)  C99  code, and the OP obviously wants to use C99. Telling him to switch to C++, a completely different language, is IMHO very bad advice.

Offline deva

  • Single posting newcomer
  • *
  • Posts: 3
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #10 on: November 10, 2011, 12:51:07 pm »

Hi friends,

Non of the your advices working. Is there any way still fix the problem. Actually I am newbie to programming. I am just teaching my self from book "C-FromNoviceToProfessional4thEd(2006)" by Ivor Horton,  Apress

I tried to compile the same code in Borland Turboo C++ compiler. Its work fine.


But i really like to use CB because its highlight the syntax in colors. So, code looks clean & good.


Any help greately appreciated.


Thanks & Regards,

Deva


zabzonk

  • Guest
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #11 on: November 10, 2011, 01:04:38 pm »
@Deva The code will definitely compile if you rename the file to a  .cpp extension, but if you are trying to learn C rather than C++ this is not the best solution, as it means things that are mistakes in C but not in C++ will not be caught. My solution of creating a console application project and supplying the -std=c99 option should definitely work - it did when I tried it myself - and is the correct way to go if you want to learn C99. Did you actually try it?

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: Problem compiling "for loop" with codeblocks compiler.
« Reply #12 on: November 10, 2011, 01:45:12 pm »
The following does not work?

Code: [Select]
#include <stdio.h>

int main(void)
{
  int count;

  printf("\n**************");       

  for( count = 1 ; count <= 8 ; ++count ) printf("\n* *");

  printf("\n**************\n");

  return 0;
}