0

//Here is the sample code to print it's input one word per line: //without bracket it doesn't work properly

#include <stdio.h>
#define IN 1
#define OUT 0

main() {
  int c, state;

  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\t' || c == '\n')
      if (state == IN) {
        putchar('\n');
        state = OUT;
      } else {
        putchar(c);
        state = IN;
      }
  }
}

//when you put brackets around the nested if block(logically a single statement) inside the first if block it works properly

#include <stdio.h>
#define IN 1
#define OUT 0

main() {
  int c, state;

  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\t' || c == '\n') {
      if (state == IN) {
        putchar('\n');
        state = OUT;
      }
    } else {
      putchar(c);
      state = IN;
    }
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    You really should indent your code to make it more readable (Run it through `clang-format` or GNU `indent` if you're not using an IDE with a reformatting feature)... which might show you the issue too. – Shawn Mar 27 '20 at 19:02
  • Thank you sir. I will not forget to indent my code from the next time. – Apurba Ghosh Mar 27 '20 at 19:10

3 Answers3

1

When you indent the code you can see the difference.

Version with no braces:

#include<stdio.h>
#define IN 1
#define OUT 0

main()
{
    int c, state;

    while((c = getchar()) != EOF)
    {
        if(c == ' ' || c == '\t' || c == '\n')
            if(state == IN)
            {
                putchar('\n');
                state = OUT;
            }
            else
            {
                putchar(c);
                state = IN;
            }
    }
}

Version with braces:

#include<stdio.h>
#define IN 1
#define OUT 0

main()
{
    int c, state;

    while((c = getchar()) != EOF)
    {
        if(c == ' ' || c == '\t' || c == '\n')
        {
            if(state == IN)
            {
                putchar('\n');
                state = OUT;
            }
        }
        else
        {
            putchar(c);
            state = IN;
        }
    }
}

In the first version, the else block is associated with the nested if, so it only prints the character when c is one of the delimiter characters and state != IN.

In the second version the else block goes with the test against the delimiter characters, so all non-delimiter characters are printed.

An else statement is always associated with the most recent if in the same block. The braces in the second version put the nested if in a block of its own, so else will not be associated with it, so it goes with the outer if.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

In the first version you outer if body is this

      if (state == IN) {
        putchar('\n');
        state = OUT;
      } else {
        putchar(c);
        state = IN;
      }

While in the second is this

      if (state == IN) {
        putchar('\n');
        state = OUT;
      }

So with the second one the else is associated with the outer if, with the first it is with the inner if.

Eraklon
  • 4,206
  • 2
  • 13
  • 29
0

In the first example, the else statement is binding the the second if statement.

if () if () {} else () {}

Which is equivalent to...

if () { if () {} else () {} }

In the second example, the else statement is binding to the first if statement.

if () { if () {} } else () {}

This occurs because the added brackets enclose the second if statement inside a block scope, where the else exists outside of that scope. Without the added brackets, the else lies within the same scope as the second if and is interpreted as a single chained statement.

SpaceKatt
  • 976
  • 6
  • 12