0
#include <stdio.h>

int main(void) {
    char c = 'y', temp;
    printf("Press y\n");
    do {
        printf("Press y to continue\n"); // read y again and again
        scanf("%c", &c); // y entered
        printf("%c", c); // loop should repeat but doesn't repeat
    } while(c == 'y');
}
ajay
  • 9,041
  • 7
  • 37
  • 68
Anish Gupta
  • 273
  • 1
  • 5
  • 18

2 Answers2

4

It will not. Because scanf reads the \n character on second iteration which cause the loop to terminate. Place a space before %cto consume this \n character left behind by previous scanf.

 scanf(" %c",&c);
haccks
  • 100,941
  • 24
  • 163
  • 252
2

Try adding a space before %c which is a scanf quirk. The space absorbs the newline char after typing the 'y'.

        scanf(" %c",&c);
suspectus
  • 15,729
  • 8
  • 46
  • 54