Copy Input to Output Using C Programming

Program description:- Write a C program for copy input to output.

For copy input to output using C programming, the getchar() function can be used. The getchar() function returns a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF, for “End of File”. 

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

Output:-

Hi, Copy it.
Hi, Copy it.

The statement,
while((c=getchar())!=EOF)
also can be written in two lines as,
c=getchar();
while (c!=EOF)

We must declare the variable c to be a type big enough to hold any value that the getchar function returns. We can’t use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int data type.

EOF is an integer defined in <stdio.h>, but it is not the same as any char value.

In this program copy input to output using C, The parentheses around the assignment within the condition are necessary. The precedence of != is higher than that of =, Which means that in the absence of parentheses the relational test != would be done before the assignment.  So, the statement 
c = getchar() != EOF is equivalent to c = ( getchar() != EOF )

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Similar C programs on String

Programs with and without using String manipulation library functions

Multi-dimensional String

Leave a Comment

Your email address will not be published. Required fields are marked *