Wednesday, August 13, 2008

I/O in c without printf and scanf.

getchar and putchar

int getchar();          
int putchar(int value);
Sample Program
#include
int main(void)
{

    int c;
    while ((c = getchar()) != EOF)
    {
        putchar(c);
    }
    return 0;

}
 Note the use of parenthesis. 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.
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

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

No comments:

Post a Comment