0

Here I have a code that should print "Error" after the first sequence of the loop, since according to my previous question Print an "Error" statement after do-while loop in C It would only read the first condition of the loop due to "short-cicuiting" and would only be executed after the first sequence of the loop... After solving that I still can't display "Error!", so I tried adding && getch() that gave me an output of

Error!

Now that I have 3(Multiple) conditions that caused the "Error!" to be executed within the first sequence, I want to know how does C respond to my multiple conditions, that might help me to solve the error.

My code:

#include <stdio.h>
#include <conio.h>

void main(){
    int inp;
    while(inp<10 && printf("Error") && getch()){
        clrscr();
        printf("Enter Number > 10: ");
        scanf("%d",&inp);
    }
    printf("Right Answer!");
    getch();
}
Community
  • 1
  • 1
Aesthetic
  • 763
  • 1
  • 14
  • 31
  • 1
    Why are you putting the `printf` and `getch` in the while-condition? They would be much more suited in the body. – Kninnug Oct 31 '13 at 13:22
  • The `printf("Error")` will be cleared by `clrscr()` and it return non-zero value always expect `printf("")` so it is irrelevant to while condition. – Majlik Oct 31 '13 at 13:46

3 Answers3

3

int inp; is not initialized. Also main should return int. Automatic variables are not initialized to a default value. Testing inp<10 like this is undefined since inp can be any junk value.

This is all you need:

#include <stdio.h>

int  main(){
    int inp = 0;
    while(inp<10 && printf("Error\n") )
    {
        printf("Enter Number > 10: ");
        scanf("%d",&inp);
    }
    printf("Right Answer!");
    getchar();
}

conio.h is not part of the C standard library. But as said by unwind in comments the above code will print Error unnecessarily once - you can redesign your code to something like:

#include <stdio.h>

int  main(){
    int inp = 0;
    while(1 ) // always true
    {
        printf("Enter Number > 10: ");
        scanf("%d",&inp);
        if(inp<10 )
            printf("Error\n");
        else
            break; // break out of while loop
    }
    printf("Right Answer!");
    getchar();
}
Sadique
  • 22,572
  • 7
  • 65
  • 91
1

According to Short-circuit evaluation for C language if you have &&, || or ? in condition it will not evaluate all conditions if it is not necessary see example:

if(0 && printf("ERROR!\n"))
  printf("OK\n");

Will never print any text because (0 && whatever) will evaluate always as false so it will not continue with second condition after && but if you will change position

if(printf("ERROR!\n") && 0)
      printf("OK\n");

Will print only ERROR! because printf("ERROR!\n") will print text and return some number greater then zero (7) so condition will be reduce to (7 && 0) what will evaluate both conditions as false but will print your ERROR! message.

When you have more && operators they will evaluate in Left-to-right order so it will looks like this:

if( ( (inp<10 && printf("Error")) && getch() ) && ... )

so it will first check if inp is less then 10 and only if its is true will continue with printing Error and so on ...

Majlik
  • 1,082
  • 1
  • 10
  • 20
0
int main(){
    int inp = 0;
    scanf("%d",&inp);


    while(inp<10 && printf("Error") && getch()){
        clrscr();
        printf("Enter Number > 10: ");
        scanf("%d",&inp);
    }
    printf("Right Answer!");
    getch();

   return 0;
}

You have to initialize/ assign value to inp before checking in the loop.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • `clrscr();` and `getch();` is not part of the C standard library, ISO C nor is it defined by POSIX. – Sadique Oct 31 '13 at 13:33