getc() != fgetc() ??

Discussion in 'C Programming' started by arun, Jul 27, 2012.

  1. arun

    arun Guest

    Hello,

    I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.

    The result of compiling the following simple program depends from the way
    it was saved.

    If it was saved with .cpp extension (default) no matter.

    If it was saved with .c extension, I get a warning "code has no effect",
    at the getc(stdin) line.

    Changing getc with fgetc no warning appears.

    #include "stdio.h"
    main() {
    printf("press Enter...");
    getc(stdin);
    return 0;
    }

    I thought getc and fgetc were the same thing. Isn't it true?
    Or the problem comes from the compiler?

    Thank you.
     
    arun, Jul 27, 2012
    #1
    1. Advertising

  2. arun

    arun Guest

    On Fri, 27 Jul 2012 20:11:50 +0000, arun wrote:
    > Hello,
    >
    > I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >
    > The result of compiling the following simple program depends from the
    > way it was saved.
    >
    > If it was saved with .cpp extension (default) no matter.
    >
    > If it was saved with .c extension, I get a warning "code has no effect",
    > at the getc(stdin) line.
    >
    > Changing getc with fgetc no warning appears.
    >
    > #include "stdio.h"
    > main() {
    > printf("press Enter...");
    > getc(stdin);
    > return 0;
    > }
    >
    > I thought getc and fgetc were the same thing. Isn't it true? Or the
    > problem comes from the compiler?
    >
    > Thank you.


    This forum is very silent! Everyone is watching the olympics or there is
    a technical fault??
     
    arun, Jul 27, 2012
    #2
    1. Advertising

  3. arun

    Eric Sosman Guest

    On 7/27/2012 4:11 PM, arun wrote:
    > Hello,
    >
    > I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >
    > The result of compiling the following simple program depends from the way
    > it was saved.
    >
    > If it was saved with .cpp extension (default) no matter.
    >
    > If it was saved with .c extension, I get a warning "code has no effect",
    > at the getc(stdin) line.
    >
    > Changing getc with fgetc no warning appears.
    >
    > #include "stdio.h"
    > main() {
    > printf("press Enter...");
    > getc(stdin);
    > return 0;
    > }
    >
    > I thought getc and fgetc were the same thing. Isn't it true?


    If <stdio.h> is included, getc and fgetc are equivalent (except
    in nit-picky aspects not relevant here).

    However, your program does not include <stdio.h> -- unless
    the compiler fails to find a "stdio.h", in which case it will try
    for <stdio.h> instead. But if it found a "stdio.h" somewhere, how
    can we tell what oddball declarations of printf, getc, and stdin
    it might be using?

    > Or the problem comes from the compiler?


    Is there actually a problem? When you run the program, does
    it complete immediately or does it wait for input? If the "problem"
    is only a warning message -- well, compilers are allowed to emit
    as many warnings as their fancy suggests.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 27, 2012
    #3
  4. arun

    arun Guest

    On Fri, 27 Jul 2012 16:30:59 -0400, Eric Sosman wrote:

    > On 7/27/2012 4:11 PM, arun wrote:
    >> Hello,
    >>
    >> I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >>
    >> The result of compiling the following simple program depends from the
    >> way it was saved.
    >>
    >> If it was saved with .cpp extension (default) no matter.
    >>
    >> If it was saved with .c extension, I get a warning "code has no
    >> effect", at the getc(stdin) line.
    >>
    >> Changing getc with fgetc no warning appears.
    >>
    >> #include "stdio.h"
    >> main() {
    >> printf("press Enter...");
    >> getc(stdin);
    >> return 0;
    >> }
    >>
    >> I thought getc and fgetc were the same thing. Isn't it true?

    >
    > If <stdio.h> is included, getc and fgetc are equivalent (except
    > in nit-picky aspects not relevant here).
    >
    > However, your program does not include <stdio.h> -- unless
    > the compiler fails to find a "stdio.h", in which case it will try for
    > <stdio.h> instead. But if it found a "stdio.h" somewhere, how can we
    > tell what oddball declarations of printf, getc, and stdin it might be
    > using?


    ???

    >> Or the problem comes from the compiler?

    >
    > Is there actually a problem? When you run the program, does
    > it complete immediately or does it wait for input? If the "problem" is
    > only a warning message -- well, compilers are allowed to emit as many
    > warnings as their fancy suggests.


    You maybe misunderstood, the compiler DOES issue a warning for getc,
    however it DOES NOT issue a warning with fgetc -- however getc and fgetc
    are supposed to be identical! How to explain this paradox?
     
    arun, Jul 27, 2012
    #4
  5. arun

    Eric Sosman Guest

    On 7/27/2012 4:27 PM, arun wrote:
    >
    > This forum is very silent! Everyone is watching the olympics or there is
    > a technical fault??


    16:11 - Original question
    16:27 - "Very silent" whine just sixteen minutes later
    16:30 - First response

    If you want faster turn-around, you'll have to purchase a
    support contract.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 27, 2012
    #5
  6. arun

    Eric Sosman Guest

    On 7/27/2012 4:34 PM, arun wrote:
    > On Fri, 27 Jul 2012 16:30:59 -0400, Eric Sosman wrote:
    >
    >> On 7/27/2012 4:11 PM, arun wrote:
    >>> Hello,
    >>>
    >>> I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >>>
    >>> The result of compiling the following simple program depends from the
    >>> way it was saved.
    >>>
    >>> If it was saved with .cpp extension (default) no matter.
    >>>
    >>> If it was saved with .c extension, I get a warning "code has no
    >>> effect", at the getc(stdin) line.
    >>>
    >>> Changing getc with fgetc no warning appears.
    >>>
    >>> #include "stdio.h"
    >>> main() {
    >>> printf("press Enter...");
    >>> getc(stdin);
    >>> return 0;
    >>> }
    >>>
    >>> I thought getc and fgetc were the same thing. Isn't it true?

    >>
    >> If <stdio.h> is included, getc and fgetc are equivalent (except
    >> in nit-picky aspects not relevant here).
    >>
    >> However, your program does not include <stdio.h> -- unless
    >> the compiler fails to find a "stdio.h", in which case it will try for
    >> <stdio.h> instead. But if it found a "stdio.h" somewhere, how can we
    >> tell what oddball declarations of printf, getc, and stdin it might be
    >> using?

    >
    > ???
    >
    >>> Or the problem comes from the compiler?

    >>
    >> Is there actually a problem? When you run the program, does
    >> it complete immediately or does it wait for input? If the "problem" is
    >> only a warning message -- well, compilers are allowed to emit as many
    >> warnings as their fancy suggests.

    >
    > You maybe misunderstood, the compiler DOES issue a warning for getc,
    > however it DOES NOT issue a warning with fgetc -- however getc and fgetc
    > are supposed to be identical! How to explain this paradox?


    Already explained, in the material you quoted.

    If you can't read the responses, I'm raising the rate on
    your support contract.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 27, 2012
    #6
  7. arun

    James Kuyper Guest

    On 07/27/2012 04:27 PM, arun wrote:
    > On Fri, 27 Jul 2012 20:11:50 +0000, arun wrote:

    ....
    > This forum is very silent! Everyone is watching the olympics or there is
    > a technical fault??


    You're thinking of this forum like it was a chat room; usenet newsgroups
    work quite differently. The time you might have to wait for a response
    is more typically hours, not minutes. You shouldn't get worried by a
    lack of response until at least 24 hours have passed. If that's too slow
    for you, usenet newsgroups are not for you.
     
    James Kuyper, Jul 27, 2012
    #7
  8. arun

    James Kuyper Guest

    On 07/27/2012 04:34 PM, arun wrote:
    > On Fri, 27 Jul 2012 16:30:59 -0400, Eric Sosman wrote:
    >
    >> On 7/27/2012 4:11 PM, arun wrote:
    >>> Hello,
    >>>
    >>> I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >>>
    >>> The result of compiling the following simple program depends from the
    >>> way it was saved.
    >>>
    >>> If it was saved with .cpp extension (default) no matter.


    It's quite possible that when you use the .cpp extension, the code
    compiles as C++ code. If that's the case, then you'll get better answers
    about why it behaves the way it does by going to comp.lang.c++.

    >>> If it was saved with .c extension, I get a warning "code has no
    >>> effect", at the getc(stdin) line.
    >>>
    >>> Changing getc with fgetc no warning appears.
    >>>
    >>> #include "stdio.h"
    >>> main() {
    >>> printf("press Enter...");
    >>> getc(stdin);
    >>> return 0;
    >>> }
    >>>
    >>> I thought getc and fgetc were the same thing. Isn't it true?

    >>
    >> If <stdio.h> is included, getc and fgetc are equivalent (except
    >> in nit-picky aspects not relevant here).


    Specifically, if you had

    FILE *files[5];
    FILE **pfile = files;

    then the expression

    getc(*pfile++);

    is allowed to cause pfile to be incremented 2 or more times, while

    fgetc(*pfile++);

    can only increment it once. As Eric said, this difference is not
    relevant to your question. However, the fact that getc() is a macro may
    be relevant to why Borland behaves differently in the two cases.

    >> However, your program does not include <stdio.h> -- unless
    >> the compiler fails to find a "stdio.h", in which case it will try for
    >> <stdio.h> instead. But if it found a "stdio.h" somewhere, how can we
    >> tell what oddball declarations of printf, getc, and stdin it might be
    >> using?

    >
    > ???


    He's just explaining why it's a mistake to use

    #include "stdio.h"

    rather than

    #include <stdio.h>

    That's unlikely to be the cause of your problem, but it's a good idea to
    correct it anyway.

    If you have trouble understanding his explanation, don't worry about it.
    Just memorize this simple rule: use #include <> for standard library
    headers, and #include "" for all other headers. It isn't really
    essential that you understand why this rule should be obeyed.

    >> Is there actually a problem? When you run the program, does
    >> it complete immediately or does it wait for input? If the "problem" is
    >> only a warning message -- well, compilers are allowed to emit as many
    >> warnings as their fancy suggests.

    >
    > You maybe misunderstood, the compiler DOES issue a warning for getc,
    > however it DOES NOT issue a warning with fgetc -- however getc and fgetc
    > are supposed to be identical! How to explain this paradox?


    As he explained above, compilers are allowed to emit warnings for any
    reason they like. In particular, they're allowed to emit warnings for
    code that has no problems whatsoever, and they're allowed to handle code
    differently, depending upon whether it uses getc() or fgetc(), even if
    they are used in equivalent ways.

    If you want to know why Borland C++ handles these two cases differently,
    you'll have to ask Borland. As far as C is concerned, there's nothing
    wrong with your code, and nothing wrong with the compiler issuing a
    warning telling you that there is something wrong with your code.
     
    James Kuyper, Jul 27, 2012
    #8
  9. arun

    James Kuyper Guest

    On 07/27/2012 04:11 PM, arun wrote:
    > Hello,
    >
    > I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    >
    > The result of compiling the following simple program depends from the way
    > it was saved.
    >
    > If it was saved with .cpp extension (default) no matter.
    >
    > If it was saved with .c extension, I get a warning "code has no effect",
    > at the getc(stdin) line.
    >
    > Changing getc with fgetc no warning appears.
    >
    > #include "stdio.h"
    > main() {
    > printf("press Enter...");
    > getc(stdin);
    > return 0;
    > }
    >
    > I thought getc and fgetc were the same thing. Isn't it true?
    > Or the problem comes from the compiler?
    >
    > Thank you.


    This message appears to be identical to one posted on 2011-06-04.
     
    James Kuyper, Jul 27, 2012
    #9
  10. arun

    Paul N Guest

    On Jul 27, 10:59 pm, James Kuyper <> wrote:
    > On 07/27/2012 04:11 PM, arun wrote:
    >
    >
    >
    >
    >
    > > Hello,

    >
    > > I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.

    >
    > > The result of compiling the following simple program depends from the way
    > > it was saved.

    >
    > > If it was saved with .cpp extension (default) no matter.

    >
    > > If it was saved with .c extension, I get a warning "code has no effect",
    > > at the getc(stdin) line.

    >
    > > Changing getc with fgetc no warning appears.

    >
    > > #include "stdio.h"
    > > main() {
    > >     printf("press Enter...");
    > >     getc(stdin);
    > >     return 0;
    > > }

    >
    > > I thought getc and fgetc were the same thing. Isn't it true?
    > > Or the problem comes from the compiler?

    >
    > > Thank you.

    >
    > This message appears to be identical to one posted on 2011-06-04.


    I found a practically identical one from Feb 2001!
     
    Paul N, Jul 28, 2012
    #10
  11. arun

    Phil Carmody Guest

    James Kuyper <> writes:
    > On 07/27/2012 04:11 PM, arun wrote:
    > > Hello,
    > >
    > > I use a Borland C++ 3.1 for Dos compiler, in Ansi C source mode.
    > >
    > > The result of compiling the following simple program depends from the way
    > > it was saved.
    > >
    > > If it was saved with .cpp extension (default) no matter.
    > >
    > > If it was saved with .c extension, I get a warning "code has no effect",
    > > at the getc(stdin) line.
    > >
    > > Changing getc with fgetc no warning appears.
    > >
    > > #include "stdio.h"
    > > main() {
    > > printf("press Enter...");
    > > getc(stdin);
    > > return 0;
    > > }
    > >
    > > I thought getc and fgetc were the same thing. Isn't it true?
    > > Or the problem comes from the compiler?
    > >
    > > Thank you.

    >
    > This message appears to be identical to one posted on 2011-06-04.


    Didn't you lot work out that he was probably a robot back then?
    That's consistent with them just being re-run, certainly.

    To be honest I don't care too much, he falls to the
    ^Organization: Aioe.org NNTP Server
    filter in my killfile.

    Enjoy continuing to waste your time on a troll,
    Phil
    --
    > I'd argue that there is much evidence for the existence of a God.

    Pics or it didn't happen.
    -- Tom (/. uid 822)
     
    Phil Carmody, Jul 28, 2012
    #11
  12. arun

    Eric Sosman Guest

    On 7/28/2012 11:15 AM, Phil Carmody wrote:
    >[...]
    > Didn't you lot work out that he was probably a robot back then?
    > That's consistent with them just being re-run, certainly.


    The original (this time around) post may have been robotic,
    but either there's an actual person watching over the robot or
    the robot's author is a better programmer than anyone I've ever
    met, heard of, or even imagined.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 28, 2012
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason K
    Replies:
    6
    Views:
    4,025
    Jeff Flinn
    May 12, 2005
  2. =?iso-8859-1?q?Jos=E9_de_Paula?=

    fgetc() past EOF

    =?iso-8859-1?q?Jos=E9_de_Paula?=, Jan 17, 2004, in forum: C Programming
    Replies:
    6
    Views:
    702
  3. William L. Bahn

    getc() vs. fgetc()

    William L. Bahn, Jul 14, 2004, in forum: C Programming
    Replies:
    13
    Views:
    1,839
    Dave Thompson
    Jul 21, 2004
  4. Bill Cunningham

    getc fgetc

    Bill Cunningham, Aug 4, 2009, in forum: C Programming
    Replies:
    7
    Views:
    803
    Keith Thompson
    Aug 5, 2009
  5. arun

    getc() != fgetc() ??

    arun, Jun 4, 2011, in forum: C Programming
    Replies:
    3
    Views:
    1,013
    Angel
    Jun 5, 2011
Loading...

Share This Page