programming style: while(TRUE), for(;;), ...?

C

CBFalconer

Mark said:
.... snip ...

then why not:
while(EXIT_FAILURE)
or
while(! EXIT_SUCCESS)
I'll tell you why not... because it's stupid! :)

A better reason is that you have absolutely no idea what those
statements will do, because you don't know the values of those
macros.
 
R

Richard Bos

CBFalconer said:
A better reason is that you have absolutely no idea what those
statements will do, because you don't know the values of those
macros.

Well, half. You don't know that EXIT_SUCCESS is zero, so you don't know
that the second loop will go on indefinitely; but you do know that the
first is an unending loop, because EXIT_FAILURE cannot possibly compare
equal to 0 (since if it were, exit(EXIT_FAILURE) and exit(0) would be
identical).

Richard
 
?

=?iso-8859-1?q?Asbj=F8rn_S=E6b=F8?=

Felix Kater said:
[...]
Second: The opened brace (often together with indentation or folding in
the editor) reminds you that your are programming on something like a
sub level which will end at some point and execute other (important)
code on the main level.

Can't you use just the braces (without the for/while) to achieve an
indented compound statement? Like this?

#include <stdio.h>

int main ()
{
printf("Numbers:\n");

{
/* Here comes a group of related statements, treat them as a unit */
printf("One\n");
printf("Two\n");
printf("Three\n");
}

/* And this is something you do afterwards */
return 0;
}


Asbj.S.
 
L

Lawrence Kirby

Why do all these tricks with the compiler?
Why not just

#define FORGET_ME_NOT

and

FORGET_ME_NOT {
code stuff
}

This doesn't provide a simple way to jump to the point after the end of
the block, in particular break; doesn't work because it provides no
enclosing loop or switch statement.

Lawrence
 
L

Lawrence Kirby

Well, half. You don't know that EXIT_SUCCESS is zero, so you don't know
that the second loop will go on indefinitely; but you do know that the
first is an unending loop, because EXIT_FAILURE cannot possibly compare
equal to 0 (since if it were, exit(EXIT_FAILURE) and exit(0) would be
identical).

There's nothing in C that prohibits this. For an example an implementation
that ignores the return value of main()/argument of exit() need make no
distinction between a success and failure value.

Lawrence
 
A

Alan Balmer

If I saw code like that I would wonder wtf you were smoking when you
wrote it. do { } while(0); is almost always the best way to express
creation of a new scope

I would prefer either the while(1) or for(;;) constructs simply
because the loop conditions are known without having to go find the
other end of the statement.
 
M

Malcolm

Christian Bau said:
Since comp.lang.c _always_ operates in the specific context of C, there
is nothing wrong at all with for (;;).
But I don't write code primarily for a comp.lang.c audience. I can assume
that the reader knows C, but he may well be a statistician with limited
programming experience, mainly in mathematical type languages. Whe he sees a
construct like for(;;) it is very offputting.
 
C

Christian Bau

"Malcolm said:
But I don't write code primarily for a comp.lang.c audience. I can assume
that the reader knows C, but he may well be a statistician with limited
programming experience, mainly in mathematical type languages. Whe he sees a
construct like for(;;) it is very offputting.

I will remember that the next time I write code with the intention of
having it read by a statistician. But for most people, writing C code
with the intention of having it read by C programmers is much more
common.
 
C

Chris Croughton

But I don't write code primarily for a comp.lang.c audience. I can assume
that the reader knows C, but he may well be a statistician with limited
programming experience, mainly in mathematical type languages. Whe he sees a
construct like for(;;) it is very offputting.

No more so than seeing the mathematically ridiculous statement x = x+1;
If anyone says thatthey "know C" and doesn't recognise for (;;) as being
a loop without a condition then they deserve to be fired because they
are misrepresenting themself.

(Yes, I have seen people come for interview who say that they are
"experienced C programmers" and don't recognise common idioms like that.
They don't get the job. I don't know who does employ them, possibly
Microsoft or the government...)

Chris C
 
G

Guillaume

Chris said:
No more so than seeing the mathematically ridiculous statement x = x+1;
Absolutely.

If anyone says thatthey "know C" and doesn't recognise for (;;) as being
a loop without a condition then they deserve to be fired because they
are misrepresenting themself.

Agree. Well, the guy was talking about someone that "may well be a
statistician with limited programming experience", though.
But that's not a valid point in my opinion. Would you submit a report
written in english to some person, maybe skilled in the field that's
dealt with in the report, but that doesn't understand half of the
english idioms? I wouldn't. Too many possibilities of misunderstandings.
So my take at this is: don't show C code to someone that doesn't know C.
That's useless. If you are going to work with someone that has "limited
programming experience", just give them the formulas and algorithms
written in natural/math language, not in C.
(Yes, I have seen people come for interview who say that they are
"experienced C programmers" and don't recognise common idioms like that.
They don't get the job. I don't know who does employ them, possibly
Microsoft or the government...)

Well, I have interviewed at Microsoft once (what was I thinking? ;-) ).
The technical interviews were actually pretty difficult; at least,
you had to solve some tricky stuff in like 40 minutes..., which, under
pressure, was too fast for me. I do remember one of the guys stating
something really stupid as to the C standard though...

(If I remember correctly, he was claiming that 'sizeof(char)'
may yield something else than 1 on some platforms. Seems to me
like 'sizeof(char) == 1' is in the standard, isn't it actually the
very definition of sizeof?.

My copy of the standard says:
"When applied to an operand that has type char, unsigned char, or signed
char, (or a qualified version thereof) the result is 1."

Oh well...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top