Why is my printf() statement being run twice?

rj1094

After the initial iteration, the printf() output is shown twice each iteration. Why?

int main()
{
  int ch;
  for( ch = ' '; ch != 'q'; ) {
     printf("Enter a character: ");
     ch = getchar();
  }
  printf("You entered a q!\n");

  return 0;
}

The terminal output is:

Enter a character: w
Enter a character: Enter a character: a
Enter a character: Enter a character: q
You entered a q!
Manav Dubey

As everyone has already stated, getchar() is consuming a newline ('\n') making you have two iterations. A way to fix this is to do this:

int main(){
  int ch;
  for( ch = ' '; ch != 'q'; ) {
     printf("Enter a character: ");
     ch = getchar();
     getchar();
  }
  printf("You entered a q!\n");

  return 0;
}

The reason for the second getchar() is to consume that newline so you won't have a double output of the same thing. Using this method will only work if you are only inputting one character.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my if statement not being called?

From Dev

Why is my onCreateView method being called twice?

From Dev

Why does my event run twice?

From Dev

Why does my module run twice in Python

From Dev

Why is the else statement not being executed while using scanf() and printf()?

From Dev

Why is my if else statement being ignored

From Dev

Why is my Print statement not being executed?

From Dev

Why is my WCF ServiceAuthorizationManager being called twice on each request?

From Dev

Why is my spring boot stateless filter being called twice?

From Dev

Why does mockito think my service is being called twice?

From Dev

Why is my elif being treated as an else statement in my bash script?

From Dev

Why does my SQL 'INSERT' statement execute twice?

From Dev

Why is my python program printing a statement twice at the end?

From Dev

Why won't ">> " print at the end of my printf statement?

From Dev

Why does inserting a printf statement make my function work correctly?

From Dev

Why does inserting a printf statement make my function work correctly?

From Dev

Why does my continuous azure webjob run the function twice?

From Dev

Why does my interceptor that is defined on two modules get run twice?

From Dev

why are the conditions in my if statement being ignored? C#

From Dev

Why isn't my return statement being recognized?

From Dev

why are the conditions in my if statement being ignored? C#

From Dev

my 2nd if statement is being ignored dont know why

From Dev

Why is viewDidLoad being called twice

From Dev

Why onclick is being triggered twice?

From Dev

Why constructor is being called twice

From Dev

Why constructor is being called twice

From Dev

Why is viewDidLoad being called twice

From Dev

How do I stop my index.php being run twice for every user

From Dev

Why is the if statement being ignored?

Related Related

  1. 1

    Why is my if statement not being called?

  2. 2

    Why is my onCreateView method being called twice?

  3. 3

    Why does my event run twice?

  4. 4

    Why does my module run twice in Python

  5. 5

    Why is the else statement not being executed while using scanf() and printf()?

  6. 6

    Why is my if else statement being ignored

  7. 7

    Why is my Print statement not being executed?

  8. 8

    Why is my WCF ServiceAuthorizationManager being called twice on each request?

  9. 9

    Why is my spring boot stateless filter being called twice?

  10. 10

    Why does mockito think my service is being called twice?

  11. 11

    Why is my elif being treated as an else statement in my bash script?

  12. 12

    Why does my SQL 'INSERT' statement execute twice?

  13. 13

    Why is my python program printing a statement twice at the end?

  14. 14

    Why won't ">> " print at the end of my printf statement?

  15. 15

    Why does inserting a printf statement make my function work correctly?

  16. 16

    Why does inserting a printf statement make my function work correctly?

  17. 17

    Why does my continuous azure webjob run the function twice?

  18. 18

    Why does my interceptor that is defined on two modules get run twice?

  19. 19

    why are the conditions in my if statement being ignored? C#

  20. 20

    Why isn't my return statement being recognized?

  21. 21

    why are the conditions in my if statement being ignored? C#

  22. 22

    my 2nd if statement is being ignored dont know why

  23. 23

    Why is viewDidLoad being called twice

  24. 24

    Why onclick is being triggered twice?

  25. 25

    Why constructor is being called twice

  26. 26

    Why constructor is being called twice

  27. 27

    Why is viewDidLoad being called twice

  28. 28

    How do I stop my index.php being run twice for every user

  29. 29

    Why is the if statement being ignored?

HotTag

Archive