0

Learning C here and I am confused.

char ch;

for(;;)
{
    puts("Type a letter: ");
    ch=getchar();
    if(ch == '~')
    {
        break;
    }
}

Why does it print 'Type a letter' twice instead of once? The program goes something like this when run:
Type a letter:
a
Type a letter:
Type a letter:
d
Type a letter:
Type a letter:
g

PS Same thing happens for while loops too.

Ab Be
  • 67
  • 1
  • 8
  • 8
    Because the newline character is a character too. – Eugene Sh. Jun 11 '18 at 16:15
  • A debugger, a break-point, and a variable watch on `ch` would make short work of discovering this. Start learning one now; it will pay off the rest of your career. Oh, an plausible duplicate here: ["Using `getchar()` on c gets the 'Enter' after input "](https://stackoverflow.com/questions/3969871/using-getchar-on-c-gets-the-enter-after-input) – WhozCraig Jun 11 '18 at 16:19
  • And note that `getchar()` returns an `int`. Not a `char`. – J...S Jun 11 '18 at 16:22
  • This is similar to the previous question [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Jun 11 '18 at 18:04

1 Answers1

0

You press 'a' and then press newline (enter). So your code takes 'a' as first input and newline as second input. So it's printing the line two times. You can try this :

char ch;

for(;;)
{
    puts("Type a letter: ");
    ch=getchar();
    getchar(); //newly added line. This line will discard the newline
    if(ch == '~')
    {
        break;
    }
}
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
  • 2
    Ahh, that makes sense. But what if a user enters more than 2 letters and then presses enter. That way your code will not work well since it's only checking for the input after the first letter, in this case, the return/enter key. – Ab Be Jun 11 '18 at 16:26
  • Yeah, actually we should write programs according to how we take inputs. If you don't press any space/enter after input then your code will work. But if you want to press those keys after every input then you need a way to discard that extra characters. – Taohidul Islam Jun 11 '18 at 16:28
  • Shouldn't this answer be accepted? – Taohidul Islam Jun 11 '18 at 16:42
  • What happens if the user just types "Enter"? Aside: `char ch; ` should be `int ch;` to match the return value from `getchar()`. It's a newbie mistake to think a character is a `char` This is shown by the output of `printf("%zu\n", sizeof 'A')` which on my 32-bit `int` machine, is `4`. – Weather Vane Jun 11 '18 at 18:05
  • On which program? @AbBe's or mine? – Taohidul Islam Jun 11 '18 at 18:08
  • in your program, since you call `getchar()` twice. – Weather Vane Jun 11 '18 at 18:09
  • Every second character will be discarded. Since I have used second `getchar()` to discard newline character for @AbBe's program. – Taohidul Islam Jun 11 '18 at 18:11
  • Can't you see that if user enters just a newline, after which the user continues to type a letter and a newline, only newlines will be kept, and the data thrown away? So `~` won't be seen unless another blank newline is typed, to get the alternation back to plan. – Weather Vane Jun 11 '18 at 18:19
  • I have noticed that. I have tried to make my code as simple as possible to questioner's help. If i try to handle all these situations then it may be tough for him. I have solve only the issue that he asked for. – Taohidul Islam Jun 11 '18 at 18:22
  • You helpfully explained the problem, but your solution is poor. – Weather Vane Jun 11 '18 at 18:25
  • Code should be easier for beginners. If we tried to add lots of things in extra those might be less helpful and more complicated for them. – Taohidul Islam Jun 11 '18 at 18:26