Frage

What is the value of putchar(c) outside loop?

main()
{
  int c;

  while (c = getchar() != EOF) {
    printf("printf c_inloop :%d\n",c);
    printf("Putchar c_inside: ");
    putchar(c);
    printf("\n\n");
  }

  putchar(c);
  printf("printf c_outloop %d",c); /*outside loop*/

}

When I run the program, say "th"(wtq) and press enter in cmd(Windows):

th
printf c_inloop :1
Putchar c_inside:(gives me a smileyface emoticon)

printf c_inloop :1
Putchar c_inside:(gives me a smileyface emoticon)

printf c_inloop :1
Putchar c_inside:(gives me a smileyface emoticon)


^Z (Ctrl + Z)
 printf c_outloop 0

Note the space before printf c_outloop 0. Is the value of putchar(c) Space?

War es hilfreich?

Lösung

When you exit the loop, the value of c is EOF, which isn't a valid character value (that's why it's used to indicate end of file). It makes no sense to pass it to putchar.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top