What does for (;;) mean?

AccruedExpenditure

Diamond Member
May 12, 2001
6,960
7
81
I'm confused on what for (;; ) just like that with no variables does... what does that do?

Also those voids are also throwing me off... any clues? Running it in the compiler with checks really hasn't helped me figure out whats going on =/

#include <stdio.h>

int main(int argc, char **argv) //declares an int argc, and a 2 dimensional char array argv
{
if (argc <= 1)
{
for (;
{
(void) putchar('y');
(void) putchar('\n');
}
}
else
{
for (;
{
int i;
for (i = 1; i < argc; i++)
{
if (i > 1)
(void) putchar(' ');
(void) fputs(argv, stdout);
}
(void) putchar('\n');
}
}
return (0);
}
 

Snapster

Diamond Member
Oct 14, 2001
3,917
0
0
The (void) for casting the return of putchar to void (unknown type), just like if you did (int) would attempt to cast to an int. If I recall putchar in some C environments returns an int indicating an error so casting to (void) would probably just allow the program to compile without you needing to assign the returned value to anything.

And yup, as above the for ( ; ; ) runs forever, maybe they should include a new loop in C#, you have for each, why not for (ever) {} Can't remember the last time I used infinite loops though, dangerous habit.
 

jalaram

Lifer
Aug 14, 2000
12,920
2
81
Wouldn't

do { } while (true);

work the same? What the advantage/disadvantage of the for version?
 

KCfromNC

Senior member
Mar 17, 2007
208
0
76
Originally posted by: Snapster
The (void) for casting the return of putchar to void (unknown type), just like if you did (int) would attempt to cast to an int. If I recall putchar in some C environments returns an int indicating an error so casting to (void) would probably just allow the program to compile without you needing to assign the returned value to anything.

It's also possible that there are other tools running to check for common mistakes in the C code (lint, for example). One of the checks would be to see that return codes aren't ignored, so for example if a function returned an error code you could do something about it in the code. Casting the function to return void lets the tool know you really mean to ignore the return code. This is commonly done for cases like putchar, printf, and so on, where you only rarely get errors, and care about them even less frequently.

 

GoatMonkey

Golden Member
Feb 25, 2005
1,253
0
0
The winking smileys in the middle of the code crack me up. Someone needs to fix this stuff if they expect this programming forum to get popular.
 
Dec 29, 2005
89
0
0
Originally posted by: jalaram
Wouldn't

do { } while (true);

work the same? What the advantage/disadvantage of the for version?


yes, the same. same as

while(true)

too. no real advantage/disadvantage AFAIK. and i'm pretty sure they all get compiled/assembled into similar instructions.
 

jalaram

Lifer
Aug 14, 2000
12,920
2
81
Originally posted by: GoatMonkey
The winking smileys in the middle of the code crack me up. Someone needs to fix this stuff if they expect this programming forum to get popular.

When you reply to a message, there's a "Attach code" button. It brings up a new dialog that says:

Code Window [ CLOSE WINDOW ]

If you would like to insert raw code into your message insert it here, the html code will be stripped out, and spaces will be replaced with HTML spaces ( ).

The OP's code becomes:

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
There's also a "Do not parse emoticons" option in the full reply Window that isn't there if you just use the quick reply at the bottom of the thread.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
And yup, as above the for ( ; ; ) runs forever, maybe they should include a new loop in C#, you have for each, why not for (ever) {} Can't remember the last time I used infinite loops though, dangerous habit.

Or they could not do that. What next you're going to want to have every variable have a damn $ in front of it.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Or they could not do that. What next you're going to want to have every variable have a damn $ in front of it.

Sigil's definitely make it easier to find variables in your code...
 

Snapster

Diamond Member
Oct 14, 2001
3,917
0
0
Originally posted by: smack Down
And yup, as above the for ( ; ; ) runs forever, maybe they should include a new loop in C#, you have for each, why not for (ever) {} Can't remember the last time I used infinite loops though, dangerous habit.

Or they could not do that. What next you're going to want to have every variable have a damn $ in front of it.

You do realise that was a tad of sarcasm there.... I'm quite happy with C#, not sure I can say the same with with C# 3.0 with var types.

 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Originally posted by: Nothinman
Or they could not do that. What next you're going to want to have every variable have a damn $ in front of it.

Sigil's definitely make it easier to find variables in your code...

Good naming and syntax conventions work well too.
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Originally posted by: Nothinman
Good naming and syntax conventions work well too.

No doubt, but I'd rather read $variable than intVarIable.

Both are quite simple to read.

I assume you are referring to Perl when you mention variables with sigils - unfortunately that language does not lend itself to readability.

Of course, the real problems only arise with programmers who have lousy programming habits.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Both are quite simple to read.

Yes, but StUdLyCaPs is damned ugly.

I assume you are referring to Perl when you mention variables with sigils - unfortunately that language does not lend itself to readability.

I assume so too, I was only commenting on smack Down's comment about the $.

perl's readability is what you make it, the only things that are a little complicated are the built-in variables with funky names like $_ but you can use the English module to let you access them with more readable names if you like and regexps but those are bad in any language.
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Originally posted by: Nothinman
Both are quite simple to read.

Yes, but StUdLyCaPs is damned ugly.

I assume you are referring to Perl when you mention variables with sigils - unfortunately that language does not lend itself to readability.

I assume so too, I was only commenting on smack Down's comment about the $.

perl's readability is what you make it, the only things that are a little complicated are the built-in variables with funky names like $_ but you can use the English module to let you access them with more readable names if you like and regexps but those are bad in any language.

Well, when I said "both" I was assuming good naming conventions - anyone who writes variable names like you did in your example should be shot

As I said before, unreadable code is the fault of the developer - I only meant that Perl makes it easier to write hard to read code, at least from what I've observed in practice.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Well, when I said "both" I was assuming good naming conventions - anyone who writes variable names like you did in your example should be shot

Well StudlyCaps is extreme but IMO CamelCase is just as bad. And the same goes for HungarianNotation that I always see in MS' crap.
 

jman19

Lifer
Nov 3, 2000
11,222
654
126
Originally posted by: Nothinman
Well, when I said "both" I was assuming good naming conventions - anyone who writes variable names like you did in your example should be shot

Well StudlyCaps is extreme but IMO CamelCase is just as bad. And the same goes for HungarianNotation that I always see in MS' crap.

HungarianNotation is horrible, I agree.
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
iDontSeeWhatIsWrongWithCamelCaseICanReadThisJustFine.

besidesYouShouldntUseExcessivelyLongVariableNamesLikeThisAnyway

- JaAG
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Originally posted by: JustAnAverageGuy
iDontSeeWhatIsWrongWithCamelCaseICanReadThisJustFine.

besidesYouShouldntUseExcessivelyLongVariableNamesLikeThisAnyway

- JaAG

However, YSUTE is just as bad...

Bill

p.s. YouShouldntUseThisEither
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |