Talk:C Programming/Program flow control

Mistakes
(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
==

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
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
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
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)

interesting bit of code
I'm not sure how to write about this for a beginner, but some mention should be made:

// this example assumes a 32-bit size_t int main(int argc, char *argv[]){ long long t = (argc) ? -1 : sizeof argv; long long i;  if(argc) i = -1; else    i = sizeof argv; return printf("t:%llx i:%llx\n",t,i); }
 * 1) include 

I get this:

t:ffffffff i:ffffffffffffffff

This has to do with type promotion. The tertiary operator version is definitely not an exact substitute for the if...else version.

Perhaps just say "mixing signed and unsigned can be bad, and remember that sizeof returns a size_t"?

AlbertCahalan 03:40, 29 September 2005 (UTC)

C programme to find the area of the Triangle: -

main (void) { float Base,Height,Area; printf("Base:"); scanf("%Base'\"); printf("Height:");
 * 1) include 

Example at end of page
What is the example at the end of the page in aid of? It seems it should be demonstrating use of goto; or at least the topic of the chapter. However, I fail to see its relevance. I suggest it be deleted.

V-a-xvi (talk) 03:29, 23 November 2010 (UTC)

for loop image
Hi. I have found this image : File:C language for.png. Is it bad image ? ( increment should be done also after last check of condition ) Am I right ? Regards --Adam majewski (discuss • contribs) 14:41, 17 March 2012 (UTC)