<p>I have been writing OO code for the last 9 years or so. Other than using messaging, it's hard for me to imagine other approach. The main benefit I see totally in line with what CodingTheWheel said: modularisation. OO naturally leads me to construct my applications from modular components that have clean interfaces and clear responsibilities (i.e. loosely coupled, highly cohesive code with a clear separation of concerns). </p> How do I fix "for loop initial declaration used outside C99 mode" GCC error?

prising that people don't pick it up.

@Sean

More significantly, since software is already fundamentally too hard for normal humans to write reliably and accurately, should we really be extolling a technique that is consistently taught poorly and appears hard to learn? If the benefits were clear-cut then it might be worth persevering in spite of the difficulty, but that doesn't seem to be the case.

374709 次浏览

Read more on for loops in C here.

There is a compiler switch which enables C99 mode, which amongst other things allows declaration of a variable inside the for loop. To turn it on use the compiler switch -std=c99

Its a programming paradigm.. Designed to make it easier for us mere mortals to break down a problem into smaller, workable pieces..

Or as @OysterD says, declare the variable outside the loop.

I've gotten this error too.

for (int i=0;i<10;i++) { ..

If you dont find it useful.. Don't use it, don't pay for training and be happy.

is not valid in the C89/C90 standard. As OysterD says, you need to do:

int i;
for (i=0;i<10;i++) { ..

I on the other hand do find it useful, so I will :)

Your original code is allowed in C99 and later standards of the C language.

rfaces of well defined classes are just simply clearer and somewhat self documenting.

REF: http://cplusplus.syntaxerrors.info/index.php?title='for'_loop_initial_declaration_used_outside_C99_mode

For Qt-creator: just add next lines to *.pro file...

QMAKE_CFLAGS_DEBUG = \
-std=gnu99


QMAKE_CFLAGS_RELEASE = \
-std=gnu99

I had the same problem and it works you just have to declare the i outside of the loop:

int i;


for(i = low; i <= high; ++i)


{
res = runalg(i);
if (res > highestres)
{
highestres = res;
}


}

This will turn C99 mode on for your Compiler.

I hope this will help someone!

HANDLEs (and the rest of the WinAPI) is OOP!

Are they, though? They're not inheritable, they're certainly not substitutable, they lack well-defined classes... I think they fall a long way short of "OOP".

or put in .bashrc (or .bash_profile on Mac):

export CFLAGS="-std=c99"

For anyone attempting to compile code from an external source that uses an automated build utility such as Make, to avoid having to track down the explicit gcc compilation calls you can set an environment variable. Enter on command prompt or put in .bashrc (or .bash_profile on Mac):

export CFLAGS="-std=c99"

Note that a similar solution applies if you run into a similar scenario with C++ compilation that requires C++ 11, you can use:

export CXXFLAGS="-std=c++11"

All too often, the class is used

should be

int i;
for (i=low...
simply for its syntactic sugar; it of both words: 1) Procedural programming without adhering to proven structured programming methodology and 2) OOP without adhering to to proven OOP methodology.

puts the functions for a record type

OOP done right is a beautiful thing. It makes very difficult problems easy to solve, and to the uninitiated (not trying to sound pompous there), it can almost seem like magic. That being said, OOP is just one tool in the toolbox of programming methodologies. It is not the be all end all methodology. It just happens to suit large business applications well.

into their own little namespace.

Most developers who work in OOP languages are utilizing examples of OOP done right in the frameworks and types that they use day-to-day, but they just aren't aware of it. Here are some very simple examples: ADO.NET, Hibernate/NHibernate, Logging Frameworks, various language collection types, the ASP.NET stack, The JSP stack etc... These are all things that heavily rely on OOP in their codebases.

Is this really it? The best bits of OOP are just... traditional procedural code? That's the big deal?

retty much be said about software engineering and design. Having the good tools at one's disposal does not necessarily confer good practices and results.