EOF

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main()
{
    char ch;
    scanf("%c",&ch);
    while(ch!=EOF)
    {
        printf("%c",ch);
        scanf("%c",&ch);
    }
}

This program gives runtime error. But if i replace scanf statement by getchar(), it runs successfully. Why?
You are misusing scanf(). You are discarding its return value (which is an int by the way). Therefore the program will not stop when the user gives it a Control-D.

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
    char ch;

    while(scanf("%c",&ch) != EOF)
        printf("%c",ch);
}


Edit: hmm.
Last edited on
Topic archived. No new replies allowed.