0

In the following code (just for experiment purpose) the fgetchar() take the value present in the system buffer and thus getting terminated , Why?

#include<stdio.h>
#include<conio.h>
int main()
{
printf("Here getch will take the value and it will not echo and will take without enter\n");
getch();
printf("Here getchar will take the value but it will echo on entering ");
getchar();
printf("Here getche will take the value but it will echo not on entering\n");
getche();/*fflush(stdin);*/
printf("now we are going to illustrate the usage of macros");
fgetchar();
}

I have read that fgetchar() works similar to getchar() the only difference being the latter is a macro, is it the only difference???

Gaurav Joshi
  • 27
  • 1
  • 6
  • 5
    C has `fgetc()` and `getchar()`, but it does not provide a standard function named `fgetchar()`. You'll have to provide some context. – John Bollinger Oct 30 '15 at 15:01
  • Regardless, it is never appropriate to flush (as in `fflush()`) the standard *input*. On some systems that might coincidentally have the side effect of flushing standard *output*, but if that's what you want then you should do it directly. – John Bollinger Oct 30 '15 at 15:13
  • The prototype of fgetchar() is present in stdio.h @JohnBollinger – Gaurav Joshi Oct 30 '15 at 15:49
  • But if I am flushing the data present in my keyboard buffer then the macro fgetchar() is working according to its sets of protocols @JohnBollinger – Gaurav Joshi Oct 30 '15 at 15:50
  • 1
    Im running ubuntu linux 14.04 and there is no man page for fgetchar and fgetchar not found in stdio.h. @GauravJoshi, what are you running that contains the fgetchar function? I also noticed the use of the conio.h header file, which is not portable and therefore should not be used. – user3629249 Oct 30 '15 at 15:53
  • @user3629249 I am running my code on gcc compiler for windows , but it is preety surprising that stdio.h does not include fgetchar() because it is a standard macro present in stdio.h (Upto my knowledge) , by the way conio.h is used merely to support getch() and getche() – Gaurav Joshi Oct 30 '15 at 16:10
  • `fgetchar()` is non-POSIX but is equivalent to `fgetc(stdin)`. – Ian Abbott Oct 30 '15 at 16:11
  • @GauravJoshi, no prototype of a function named `fgetchar()` is declared in any header file installed on my system, much less in my `stdio.h`. I say again: it is *non-standard*. – John Bollinger Oct 30 '15 at 22:11
  • @GauravJoshi, Flushing, as in `fflush()`, is about forcing buffered *output* out to its ultimate destination, to ensure it gets there. It has nothing whatever to do with your program's *inputs*. Your own program is the ultimate destination of its inputs; to make sure the input data arrive you simply read them. – John Bollinger Oct 30 '15 at 22:17

1 Answers1

0

to my knowledge, the only difference between fgetchar() and getchar() is fgetchar() is a macro that calls the system function fgetc()

Here is what the man pages say:

   fgetc() reads the next character from  stream  and  returns  it  as  an
   unsigned char cast to an int, or EOF on end of file or error.

   getc()  is equivalent to fgetc() except that it may be implemented as a
   macro which evaluates stream more than once.

   getchar() is equivalent to getc(stdin).
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • In C there is no provision to take input and output , Ritchie had intentionally left it , Every Operating system has its own ways of taking input and output from the user and the various C compilers which are known to us have codes(Standard Library functions) written in them which uses the power of OS to take input and output in C , therefore the word stream includes the stream of OS , Is it because of this , it requires to flush the stream??? – Gaurav Joshi Oct 30 '15 at 16:15
  • @GaurovJoshi maybe the library functions mentioned in the first edition of [K&R](https://en.wikipedia.org/wiki/The_C_Programming_Language) weren't considered part of the language, but they are since ANSI/ISO standardization. – Ian Abbott Oct 30 '15 at 16:49
  • @user3629249 I think the point of `fgetchar()` is that it is never implemented as a macro, i.e. `fgetchar()` is to `fgetc(stdin)` as `getchar()` is to `getc(stdin)`. But of course `fgetchar()` is non-standard, so all bets are off. Just saying it would be pointless having `fgetchar()` as a macro because `getchar()` can be implemented as a macro. – Ian Abbott Oct 30 '15 at 16:55
  • A google search indicated that `fgetchar()` is (usually) implemented as a macro that invokes `fgetc()` with a parameter of `stdin`. – user3629249 Oct 30 '15 at 17:23