-3

I know the program I am posting is very basic but I am unable to find the error. I rechecked everything but despite no compilation error, the output is not coming.

#include<stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    while(1)
    {
        if((scanf("%d",&i))!=42)
        {
            printf("%d",i);
        }
        else
        {
            break;
        }
    } 
    return 0;
}  

Thank You for your time and consideration.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
user3118438
  • 87
  • 1
  • 5
  • 2
    Ugh - please indent your code correctly. – Jonathon Reinhart Dec 19 '13 at 09:05
  • Is the problem that the code runs in an infinite loop? What are you trying to achieve? – Kevin Holditch Dec 19 '13 at 09:06
  • 1
    1. No proper formatting, 2. No error description, 3. No input, 4. No expected output. – Devolus Dec 19 '13 at 09:06
  • 2
    @NishithJainMR Indentation is OK, but please *do not* modify the actual code. – Jonathon Reinhart Dec 19 '13 at 09:18
  • Guys first of all thanks a lot for your help. I got the answer. I am really sorry that I made a few mistakes while posting. Actually I am new here so I didn't notice the correct way of posting. In future I will take care of all this when posting questions. Thanks a lot – user3118438 Dec 19 '13 at 09:22
  • @JonathonReinhart: Sorry brother... I manually started indenting the code. Sorry again... –  Dec 19 '13 at 09:23
  • @NishithJainMR No problem, it was just a very curious edit (the added space in the `scanf` format string) that *could* have impacted the meaning of the question. – Jonathon Reinhart Dec 19 '13 at 09:29
  • :-) Yes I know, even I didn't notice that I added a space in there... –  Dec 19 '13 at 09:34

2 Answers2

7

scanf does not return the value entered. It returns the number of inputs matched. So your check for 42 is incorrect. It will return 1 if an integer was assigned to i, or 0 if there was invalid input. If you want to exit the loop when the user enters 42, you should explicitly test if (i == 42).

As simonc mentioned, The reason you're not getting any output is because printf("%d",i); is not printing a newline "\n" after the number, and the standard out (stdout) stream is line-buffered. That means the standard library is going to continue to buffer up your printfs until a newline is encountered. At this point the buffer will be flushed to the console.

The solution is to add the newline (which is probably what you desire anyway): printf("%d\n",i); Alternatively, you could call fflush(stdout) to tell the std library to flush the buffered content to the console immediately. Or, writing to stderr will output immediately, because it is unbuffered.


This should demonstrate:

#include <stdio.h>

int main()
{
    int i;
    while(1) {
        fprintf(stderr, "Enter a number (or 42 to quit): ");
        if((scanf("%d",&i)) != 1) {
            fprintf(stderr, "Invalid input.\n");
            continue;
        }
        if (i==42)
            break;

        printf("Number provided: %d\n", i);
    }
    return 0;
}

Output:

$ ./a.exe
Enter a number (or 42 to quit): 17
Number provided: 17
Enter a number (or 42 to quit): 42
$ 
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • I think the question is asking why nothing is printed to stdout ("the output is not coming"). Isn't this because of the lack of a newline in the format string passed to `printf` (and/or lack of `fflish(stdout)`? – simonc Dec 19 '13 at 09:09
0

You are comparing the scanf function's return value to 42, not the variable i's value. maybe you should read this http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf

George
  • 155
  • 1
  • 1
  • 6