Wednesday, August 13, 2008

I/O in c without printf and scanf.

Problem
I/O in c without printf and scanf.
Method 1 - getchar and putchar
int getchar();
int putchar(int value);

Sample Program
#include <stdio .h>
int main(void)
{
    int c;
    while ((c = getchar()) != EOF)
    {
        putchar(c);
    }
    return 0;
}

Note the use of parentheses. c=gethar()!=EOF implies whether c = 0 or 1 depending on whether character get is not EOF or is EOF. EOF is 257 , so use int rather than char.


Method 2 - gets and puts
gets(name of string) ;
int puts(name of string);
for puts - It terminates the line with a new line, '\n'. It will return EOF is an error occurred. It will return a positive number on success.
Sample code
char name[60];

if ((pt = gets(name)) == NULL)
{
    printf("Error on read\n");
    return(1);
}
puts(name);

Thanks

0 comments:

Post a Comment