Hey hi ,

i want to read input frm user fm command prompt(Cmd)!!And aslo print it on Cmd.

Now if i use

#include <stdio.h>

main()
{ int c;
 while ((c=getchar()!=EOF)//get the sinle char
  putchar(c);//prnt it on cmd
}

But i dnt knw how to enter EOF as i am user and wants to end somewhere!!!!


How to do it???Whts the value of EOF????

Recommended Answers

All 7 Replies

EOF isn't a character, it's a flag returned by getchar denoting end-of-file. You can signal end-of-file from the command prompt (assuming Windows) with the Ctrl+Z keyboard combination.

If you want to print the value of EOF, I'd suggest using printf:

printf("EOF: %d\n", EOF);

Hello,
I would suggest you to read the specification of getchar().
But anyway...
In a regular case using getchar() EOF is returned when a reading error happens.
So checking for it is done to detect errors.
Your code could be rewritten as the following, I hope it gets clearer this way for you.

main()
{ int c;
while(1)
{
// read a character
c= getchar(); 
// check if there was an error
if( c== EOF)
//if there was, break out from the loop
break;
//ff there wasn't,print the char 
putchar(c);
}
}

Also, there is a problem with your code.

while ((c=getchar()!=EOF)

The line above does the following:
Assigns zero to c if there was an error reading from the standard input and one if reading was successful.
To achieve the desired result,you should implement parentheses like this:

while ( ( c= getchar() )!=EOF )

This way the program runs until there is an error in reading from the standard input.

To answer your other question, you can break out of a loop using the break statement.
E.g. to read until an 'a' is inputted use the following code:

1.
      #include <stdio.h>
      main()
      { int c;
      while ((c=getchar())!=EOF)
      {
      // exit the loop if an 'a' is read.
      if(c=='a')
      break;
      putchar(c);
      }
      }

Correct me if I'm wrong ,but i suppose you should read a tutorial on C.
Good luck with studying.

Andrew

Now if i use

#include <stdio.h>

main()
{ int c;
 while ((c=getchar()!=EOF)//get the sinle char
  putchar(c);//prnt it on cmd
}

then your program will not compile because of the unbalanced parentheses in line 5.

EOF is added by the compiler at the end of each file not by user ok sir. so dont b sad

commented: bad information - EOF is a constant in stdio.h -4

At run time just enter cntl+z which equals to end of file (EOF) character.

At run time just enter cntl+z which equals to end of file (EOF) character.

Assuming you meant ctrl+z.
When typed to a Win32 command line, it actually signals EOF.
However, typed to a terminal under a *nix system, the current foreground process is sent a
SIGTSTP signal (which by default, suspends the process).
And actually Ctrl+D is the combination,which signals EOF,under *nix when typed to a terminal.

EOF is added by the compiler at the end of each file not by user ok sir. so dont b sad

Wrong. The compiler has nothing to do with EOF. The compiler only converts the program source code to binary so that the operating system can execute it, and does nothing to the contents of a file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.