Talk:C Programming/Program flow control/Archive 1
Add topic
Mistakes
[edit source](e.g.: you reset the lesser of a and b to 0 after the comparison) should be greater
(when EITHER a or b is 1) should be not zero or true
CONCEPT USED BEFORE INTRODUCTION====
[edit source]The `enum` type is used in this chapter, but has not been introduced yet. (In the chapter on data types, only the `char`, `int`, `float`, `double`, `short`, `long`, and `unsigned` are introduced.)
"breaking outside of deeply nested loops can be done without the use of the goto statement."
How can this be done without using goto? Did you mean changing the conditions in the outer loops to see if we've used a break in an inner loop?
No, it's simpler than that.
The 'continue' keyword can be used to jump to the top of the loop. The 'break' keyword will jump to the first executable statement after the loop. Both keywords can only modify the thread of control in the current loop, that is, a 'break' inside loop A, nested in another loop B will only move control to B, the outer loop.
A more obscure (opaque?) technique uses the setjump/longjump keyword pair, but setjump/longjump is normally way scary.
- Yeah, so, you can't do it. Don't be so afraid of goto. Linux 2.6.11.11 uses it 29751 times in 4693521 lines of code. (the *.c files, not *.h files) That's 1 out of every 156 lines or 0.6338 percent, and I'm not excluding comments or blank lines. If I look only at lines containing a { or ; character, I find that 1 out of every 67 lines have a goto, which is 1.48 percent. The reason is error handling with proper clean-up, freeing memory allocations and releasing locks without massive overhead and/or unnessesary state variables. AlbertCahalan 23:45, 28 September 2005 (UTC)
goto
[edit source]Here you can see the relative popularity of various control keywords in the Linux 2.6.11.11 kernel. Remember that this represents a large number of contributers producing a highly-regarded piece of software that people are betting their businesses on.
KEYWORD COUNT PERCENT do 2679 0.6728 switch 11314 2.8414 while 12358 3.1036 for 27180 6.8259 goto 29107 7.3099 else 35416 8.8943 if 280134 70.3522
Yes folks, the goto is second only to if and else. Your professor lied.
AlbertCahalan 04:15, 29 September 2005 (UTC)
- As seen in the Linux kernel, goto is a rather popular mechanism for primitive exception handling. So much so that if state machines merit a mention then this should, too. Of course being that the main wikipedia has a NPOV policy, perhaps this wikibook should follow something similar, i.e. neutrality to what constitutes a good indent style, goto policy and so forth? 85.76.93.185 12:09, 22 April 2006 (UTC)
getchar
[edit source]Edited the While section of this page because getchar() reads the next character from stdin, not necessarily the next character from the keyboard. On most systems stdin is populated only when the Enter key is pressed, so multiple prompts appear (getchar() gets called more than once for each input).
TRUE and FALSE
[edit source]C does not define these. Because truth is not represented by any one single value, it is considered very bad style to define these. People sometimes do of course, leading to all sorts of horrid bugs. When I'm faced with fixing up C code that defines these, my immediate thought is that the code will be riddled with bugs because the author did not accept the C way of doing things. The C way might be gross by modern tastes, but you're far better off just learning to accept it. AlbertCahalan 23:49, 28 September 2005 (UTC)