tags:

views:

255

answers:

4

If I write a C program then it will not automatically get out of if else like ....

#include<stdio.h>
int main ()
{
  int a, b, c, d;

  printf ("enter the value ");
  scanf("%d %d %d ",&a,&b,&c);
  d=a+b+c;
  if(d==180)
    printf("triangle is valid ");
  else 
    printf("triangle is invalid ");
  return 0;
}

then it will not terminate itself.....

Can anyone help to figure out what the problem in this .....

+7  A: 

It's the space at the end of the scanf format string. Remove that space and your program will terminate.

Can Berk Güder
This requires an explanation: a space in scanf format means that it should read as much whitespace (spaces, newlines, etc) as is available. Because there is one at the very end, it will just keep reading until a non-whitespace character is input. The best solution is not to use spaces in the format string at all, unless you explicitly need to read whitespace. Notice that most formatters (such as %d) automatically read and discard all preceding whitespace in the input.
Tronic
In other words, this has nothing to do with the if-statement.
Chuck
+5  A: 

I guess there is inconsistency between the scanf() format string and the format you enter your data in. But seriously, you should accept some old answers before asking new questions.

SF.
+3  A: 

Omit the spaces in the scanf string

scanf("%d%d%d",&a,&b,&c);
A: 

scanf function normally skip the space between the inputs.

In your code you ask the input in the following format

   scanf("%d %d %d ",&a,&b,&c);

It is represent the 1input as 1 space,1input and 1 space, 1input and 1 space.

So if you give the three input after the enter, it will skip the new line also.

Because scanf function will take the input as non white space character.

To avoid this you need to give the 4 input. So that time, the first three inputs are stored in the variable a b c, Then next space and values are stored in the buffer.

After run the program you need to give the input like the folllowing 
   12 12 12 12 

Here the first three inputs are stored in the a b c variables.

Otherwise your scanf format should be the following format

scanf("%d%d%d",&a,&b,&c);
muruga