Thread: putc() != fputc() ?

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    putc() != fputc() ?

    I'd like to ask what's the difference between library functions
    putc() and fputc(). Their functional explanation in my C
    reference is EXACTLY the same. The only difference is this
    sentence in fputc(), "Even though ch is declared to be an int
    for historical reasons, it is converted by fputc() to an
    unsigned char."

    So,
    1) Aren't the two functions supposed to be exactly the
    same and perform exactly the same operations ?

    2) When fputc()'s ch argument

    is converted to an unsigned
    char since it is declared as an int in its prototype because
    character arguments are elevated to ints at the time of the call ?
    int fputc( int ch, FILE *stream );

    3) The same facts apply in another the case of lib functions
    fgetc() and getc(). Any comments on this, too ?

    thanks,
    trekker
    to boldy code where...

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    They are identical, but both exist "to preserve compatibilitywith older versions of C" according to Herbert Schildts 'C The complete reference'.

    Mind you, this book is widely regarded as being next to useless by many contributors to this board!

    foffo
    'C that?... I felt nowt' - The Original Foffo Spearjig and his hard dog. The Tube TV show, sometime in the 80's

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    fputc is the primitive function for which the behavior of other input functions is based off of. Whether or not fputc is actually used in these functions is irrelevant. fputc is usually a function but can be masked with a macro.

    putc was traditionally a macro mask for fputc, but it is also required to be implemented as a true function. The difference between putc and fputc is that by using putc, you risk running the macro version which is inherently unsafe because it may have to evaluate its stream argument more than once. This causes complications that most people aren't aware of and thus do not watch out for, so fputc is better to use. fputc's macro does not have this problem.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-11-2009, 11:52 PM
  2. fputc fgetc file incomplete reed
    By kryptkat in forum C Programming
    Replies: 7
    Last Post: 12-11-2007, 09:20 PM
  3. Help with fscaf() to getc() and fprintf() to putc()
    By otchster in forum C Programming
    Replies: 14
    Last Post: 10-30-2005, 11:58 PM
  4. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  5. putc question
    By poorman in forum C Programming
    Replies: 2
    Last Post: 07-10-2002, 08:45 AM