Which to use for C?

Which to use for C?

Other urls found in this thread:

godbolt.org/g/hLaEj8
godbolt.org/g/oC2Y9D
stackoverflow.com/a/37292153
en.wikipedia.org/wiki/Rhetorical_question
en.wikipedia.org/wiki/I_dont_care
pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdbool.h.html
twitter.com/NSFWRedditImage

while is more understandable for others reading your code
they compile to the exact same thing anyway

is there any difference between a while loop and a for loop at the assembly level

for (;;), it looks cooler

while (true)

>for (;;)
Is considered the most appropriate for infinite loops

for is more complexy

do{

}while(true);

while(OP == gay)

the code is literally crying when you use that for loop

This

that code will never execute, heh

[citation needed]

Why write so much more to express the same thing?

Because you're an actual developer instead of some Sup Forums sperg and as such realize the importance of readable code

How is
do {
/* ... */
} while (true);
more readable than
while (true) {
/* ... */
}
?

in my field, readability and maintainability are much more important than short term development efficiency - as my code will be in use for a long time. not withstanding those concerns, writing that expression is as simple as typing "do" and hitting tab - so it's not really worth worrying about writing more.

those two control structures are not equivalent, test it for yourself :)

#define EVER ';;'

for(EVER){}

reported

Ill preface this by saying it was totally fucked and I dont know why it happed,

BUT I once had a situation where while(1) did not actually execute or loop. I tried changing all sorts of shit, eventually replacing the 1 with while(2 < 3) and that change alone caused the entire program to function as expected.
Ever since them Ive been sorta phobic about using just 1.

int main(void) {
do {
printf("Hello, world!\n");
} while (1);
}

0000000000400526 :
400526: 55 push %rbp
400527: 48 89 e5 mov %rsp,%rbp
40052a: bf c4 05 40 00 mov $0x4005c4,%edi
40052f: e8 cc fe ff ff callq 400400
400534: eb f4 jmp 40052a
400536: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
40053d: 00 00 00

int main(void) {
while (1) {
printf("Hello, world!\n");
}
}

0000000000400526 :
400526: 55 push %rbp
400527: 48 89 e5 mov %rsp,%rbp
40052a: bf c4 05 40 00 mov $0x4005c4,%edi
40052f: e8 cc fe ff ff callq 400400
400534: eb f4 jmp 40052a
400536: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
40053d: 00 00 00

int main(void) {
for (;;) {
printf("Hello, world!\n");
}
}

0000000000400526 :
400526: 55 push %rbp
400527: 48 89 e5 mov %rsp,%rbp
40052a: bf c4 05 40 00 mov $0x4005c4,%edi
40052f: e8 cc fe ff ff callq 400400
400534: eb f4 jmp 40052a
400536: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
40053d: 00 00 00

hmm...

Every time I type "while( true)" I ask myself whether or not I'm going about the problem in a retarded way and 19 times out of 20, I am.

for (;;) has less characters, therefore it's better.
You can't prove me wrong.

i feel this way whenever I use goto, but there's many justifiable reasons to write things as an infinite loop, or simply as while(!ptr->locked);

int main(void) {
while(0) {
printf("Hello, world!\n");
}
}

int main(void) {
do {
printf("Hello, world!\n");
} while(0);
}


?

The compiler will make all these things into the same thing.

iirc for (;;) gets more reliably optimised to an unconditional jump at low optimisation levels like -O0 but at that level do you really care?

for (;;) compiles to an unconditional jump instead of checking the "while" condition at the end of each iteration, this makes it a bit faster if your loop is particularly tight

int main(void) {
while(0) {
printf("Hello, world!\n");
}
}

00000000004004d6 :
4004d6: 55 push %rbp
4004d7: 48 89 e5 mov %rsp,%rbp
4004da: 90 nop
4004db: b8 00 00 00 00 mov $0x0,%eax
4004e0: 5d pop %rbp
4004e1: c3 retq
4004e2: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
4004e9: 00 00 00
4004ec: 0f 1f 40 00 nopl 0x0(%rax)

int main(void) {
do {
printf("Hello, world!\n");
} while(0);
}

0000000000400526 :
400526: 55 push %rbp
400527: 48 89 e5 mov %rsp,%rbp
40052a: bf c4 05 40 00 mov $0x4005c4,%edi
40052f: e8 cc fe ff ff callq 400400
400534: b8 00 00 00 00 mov $0x0,%eax
400539: 5d pop %rbp
40053a: c3 retq
40053b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

Are you really suggesting that a compiler wouldn't interpret "while( literal_constant_that_equates_to_true )" as an unconditional jump?

Leave the micro-optimizing to the compiler.

to be honest when are you realistically going to absolutely need the extra cycle or two per iteration that that would net you?

and at -O2 both the other loops compile to an unconditional anyway

My point demonstrated here and in pic related.

use an event loop you retarded faggot

when user said the compiler would make them into the same thing he was clearly referring to while(true) not the control structure in general

also why is your font rendering so fucking hideous
is that windows?

>Which to use for C?
If you're working on an existing project: whatever is most consistent with the rest of the code.

If it's your own, do whatever is most comfortable.

But we're talking about infinite loops, not no-op loops.

that's rather silly then since he responded to me specifically saying the control structures were different. and yeah it's windows, post a comparison pic: godbolt.org/g/hLaEj8

...

pretty much no

wrong

link to godbolt like a real programmer godbolt.org/g/oC2Y9D

we're not using the same font, so it's not the best comparison, but yeah the hinting is definitely better on yours. maybe i should fiddle with a new font renderer.

This is both incorrect and inconsequential.

( ; ; ) is a kabutops

A kabuto!!! I'm really sorry guys

this at least seems like the same font
why is antialiasing so bad on windows? it seems to be using subpixel (or at least trying to)
how hard is it to fix on windows? I didn't even know you could change it

#define while (x) for (;(x);)

Why doesn't everyone do this?

>(;(x);)
crying bear emoji ;~;

label: flag
//
goto label;

"1" doesn't necessarily evaluate to "true" in all languages.

we're talking about C

1 isn't true in all C

Even in C it's just a convention.

does if not necessarily branch if non-zero?
does using boolean operators not necessarily return 1 if true, zero if false?
genuine question, I have honestly no idea what's lurking in the depths of the C standard

Maybe but isn't it guaranteed that zero=false, nonzero=true?

while sleep 1; do
body
or
while true; do
body
sleep 1

No

the only answer

Yes, it is guaranteed.

Where

>in my field, readability and maintainability are much more important than short term development efficiency
So your work is not important whatsoever? Good to know.

t. neet

stackoverflow.com/a/37292153

forever $ do

#define ever ;;
for(ever)

while(1), it's conventional.

#define ;; EVER
for(EVER)

error: 'true' was not declared in this scope

One of them exec printf before checking the condition, try to guess which one...

en.wikipedia.org/wiki/Rhetorical_question

Shaving off a few microseconds of execution time and looking cool on github/stackoverflow are not important.
Security and maintainability however, are. Your unreadable chicken scratch will do nothing but bring the rest of your team down and hold back progress by making it more difficult to review and update when necessarily.

As we've already shown ITT, the compiler doesn't give a shit 90% of the time and your moronic braindead ass stringing bloated libraries together while not knowing the difference between big-O and your own asshole will be a far greater detriment to the all-important performance of your product than being polite to future maintainers and ensuring your work will continue to survive and benefit customers and developers long after your likely short career in development (if anyone would actually hire your retarded ass in the first place) has come to an end.

en.wikipedia.org/wiki/I_dont_care

How do you see what the C code is compiled to? I have wanted to do that many times

compile to object code, then disassemble with objdump -d

alternatively just use gcc.godbolt.org
it only accepts C++ compliant C tho.

gcc -S program.c

Alt F8 in visual studio.

>gcc.godbolt.org
Thanks, i forgot this website.

#define forever for(;;)

forever {
...
}


t. K&R

top
// ...
goto top;

for (;;)
Anything else is bloat.

>heh
Are you sure?

They are absolutely equivalent if the true is a constant.

And the do is no more readable than the other one anyway. 'do' in C-style languages generally signals that something funny is coming up. Using it when something funny is not coming up only cues the reader to start looking for possible funny stuff that isn't there, wasting their time.

Lame. Everyone knows what ;; means in a for loop, so just use it. Stupid affectations in code are the type of thing you do when you're new enough that that affectation is just on the edge of your zone of development and understanding, so you feel all accomplished about using it.

They may have wrote that in the book, but they never actually used it themselves. See .

>for C

Yeah but those are not equivalent.

typedef enum {false, true} bool;

?

hell: goto hell;

#define (^_^)"

...

i think there was some confusion. i won't use a do-while if it isn't necessary, but that's the most verbose between the two reasonable options for indefinite loops so i picked it since you can infer way I write a while statement from it.

when he said "all that" i assumed he meant compared to for(;;)

the code is equivalent, but that's not what i meant, which is why i said control structures.

Just do#define loop for(;;)
loop {
puts("You thought here would be a hello world, but instead your mother will die in her sleep, unless you post a blocker.");
break;
}

Come back when you know C, my dear freshmen.

(;__;)

pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdbool.h.html
maybe you should learn C first? im sure you can do it if you try hard enough and apply yourself :)

> C99
K+R C or GTFO