Thread: Strange effect of braces (K&R chap 1)

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Strange effect of braces (K&R chap 1)

    Hi,

    I've read that extra braces won't have an effect on compiling code, but when I ran a piece of code through my compiler in Ubuntu with an extra set of braces (it's a step toward completing an example from K&R, 1.17), the program seemed to repeat itself several times over.

    I've experimented removing them and adding them and it always has the same effect of making the code run several times. Any idea what could be happening?

    EDIT: here is the code

    A - Working normally
    Code:
    #include <stdio.h>
    
    #define IN 1
    #define OUT 0
    #define MAXLINE 100
    #define MAXWORD 100
    
    main()
    {
    	int i, x, c, b, nline, nword, state, pass;
    	int wdcount[MAXWORD];
    	state = OUT;
    	i = x = nline = nword = 0;
    	while ((c = getchar())!= EOF) {
    		if (c != '\n'){
    			if (c != '\t' && c != ' ' && state == OUT){
    			++nword;
    			state = IN;
    			}
    			else if (c != '\t' && c != ' ' && state == IN){
    			;
    			}			
    			else if (c == '\t' || c == ' ' && state == IN){
    			state = OUT;
    			}
    			else if (c == '\t' || c == ' ' && state == IN){
    			;
    			}
    			}
    		
    		
    		else if (c == '\n'){
    			wdcount[i] = nword;
    			nword=0;
    			++i;
    			state = OUT;
    			}
    
    		
    			}
    			
    		for (x = 0; x <i;++x){
    		printf("%d\n", wdcount[x]);
    			}
    		printf("Yankee\n");
    	}
    B - Extra Braces before if, at end of printf;

    Code:
    #include <stdio.h>
    
    #define IN 1
    #define OUT 0
    #define MAXLINE 100
    #define MAXWORD 100
    
    main()
    {
    	int i, x, c, b, nline, nword, state, pass;
    	int wdcount[MAXWORD];
    	state = OUT;
    	i = x = nline = nword = 0;
    	while ((c = getchar())!= EOF) {
    		{if (c != '\n'){
    			if (c != '\t' && c != ' ' && state == OUT){
    			++nword;
    			state = IN;
    			}
    			else if (c != '\t' && c != ' ' && state == IN){
    			;
    			}			
    			else if (c == '\t' || c == ' ' && state == IN){
    			state = OUT;
    			}
    			else if (c == '\t' || c == ' ' && state == IN){
    			;
    			}
    			}
    		
    		
    		else if (c == '\n'){
    			wdcount[i] = nword;
    			nword=0;
    			++i;
    			state = OUT;
    			}
    
    		
    			}
    			
    		for (x = 0; x <i;++x){
    		printf("%d\n", wdcount[x]);
    			}
    		printf("Yankee\n");}
    	}
    Last edited by montka; 06-25-2011 at 03:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course braces have an effect.
    Code:
    if( a == b )
        this();
    notthis();
    Compared to:
    Code:
    if( a == b )
    {
        this();
        andthis();
    }
    Without seeing what you are actually doing, and without a better explanation on your part, all we can do is make wild guesses. Normally I'd use my ESP to see what it is you are doing over there, but I haven't had my coffee yet.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    I'd added brackets to everything within a while statement accidentally and was having trouble compiling the code. Once I realized they were unnecessary and removed them I wondered how two brackets could have made the code run so strangely.

    Sorry, to start from the beginning, this piece of code exists to count words per line ina given input. Once each line ends, the word count is assigned to array wdcount[i] and the counter i returns to 0. Each value of wdcount[] is then printed.
    I added in the word Yankee at the end because with the extra brackets that existed at first, the counter was looping many times.

    Now having looked at it again I can see that the printf() function needed to work after the while loop had finished. Adding the brackets made it print for every character of the input document.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by montka View Post
    Now having looked at it again I can see that the printf() function needed to work after the while loop had finished. Adding the brackets made it print for every character of the input document.
    Of course braces mark the beginning and end of a block of code. They also mark the beginning and end of a "scope" within your code. They have effects on the program's stack and they affect the way the compiler composes loops and conditional statements.... Even the extra braces in your else if() statements, containing only a semicolon will have an effect.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your indenting style is also going to make things more difficult for you, and others who study your code.

    If you are indenting some dependent lines of code, but leaving others such lines of code not indented, it's just that much more work to understand your code.

    Why do this?

    Code:
    while(someTestHere) {
       if(anotherTestHere) {   //an indented dependent line of code - Good!
       actionToTakeIfAnotherTestIsTrue; //an unindented dependent line of code. Poor Style.
       }
    }
    
    //When this is so much clearer:
    while(someTestHere) {
       if(anotherTestHere) {   //an indented dependent line of code - Good!
          actionToTakeIfAnotherTestIsTrue; //an indented dependent line of code. 
       }
    }
    As you code more, your eye becomes trained to catch irregularities in your code . If you use a very consistent style of indenting dependent lines of code, then your coding will improve, and others will understand it easier.
    Last edited by Adak; 06-26-2011 at 04:46 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Thanks for the tip!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is curly braces here is a must?
    By mashour06 in forum C Programming
    Replies: 8
    Last Post: 04-25-2009, 04:41 PM
  2. Strange effect on scrollbars
    By Niara in forum Windows Programming
    Replies: 1
    Last Post: 09-27-2006, 05:10 AM
  3. braces
    By swgh in forum Tech Board
    Replies: 27
    Last Post: 01-28-2006, 09:27 AM
  4. Highlighting Braces...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-15-2005, 01:59 PM
  5. What Do Braces Do?
    By jrahhali in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2004, 04:14 PM