#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    putc and fputc - and other confusion


    1) What is the different between putc and fputc
    2) Unless you are using a buffer, Is it true that fprintf is better than fputs ?

    3) C also has a bunnch of other get and read type as well
    and it can get confusing at time. I am trying to stick to one
    standard.

    =======================================
    #include <stdio.h>
    #include <stdlib.h>

    /*******************************************/

    start_processes()
    {
    FILE *file_pointer, *fopen();
    char ETH1[13];

    file_pointer = fopen ("/etc/yellowbox/network-config", "r");
    fscanf(file_pointer, "%*[^=] %*c %s", ETH1); /* --> ETH1 = 192.168.20.1 */

    /******************************************/

    file_pointer = fopen ("/root/file_putc", "w");
    putc ('5', file_pointer); /* putc write only one character to a file */

    file_pointer = fopen ("/root/file_fputc", "w");
    fputc ('3', file_pointer); /* fputc write only one character to a file */

    /*****************************************/

    puts ("puts write many characters to the monitor.");
    puts ("putchar write only one character to the monitor.");

    putchar ('p');
    printf ("\n");

    /*****************************************/

    file_pointer = fopen ("/root/file_fputs", "w");
    fputs ("fputs write many characters to a file", file_pointer);

    file_pointer = fopen ("/root/file_fprintf", "w");
    fprintf (file_pointer, "fprintf write many characters to a file");

    fclose(file_pointer);
    }

    /*****************************************/

    main()
    {
    start_processes();
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    1. http://www.openbsd.org/cgi-bin/man.c...86&format=html See the DESCRIPTION section for the differences.

    2. Never heard that one before. I guess it depends on what you mean by "better". If you're talking about printing strings, I would have thought it was the other way around --- I've actually used puts() instead of printf() in a Terminate and Stay Resident program and the code ended up smaller, since puts() only deals with strings. In fact, you can verify the same thing yourself, using fputs() and fprintf().
    Code:
    #include <stdio.h>
    int main(void) {
      fputs("Hello world", stdout);
      /*fprintf(stdout, "Hello world");*/
      return 0;
    }
    Compiling the above code twice with the command line (-static is to tell the compiler to statically link the routines (fputs and fprintf) into the executable, instead of dynamically linking to the libs at runtime):
    gcc -static -o test test.c

    We get:
    Using fputs(): 21746 bytes
    Using fprintf(): 53194 bytes

    So, obviously, the code for fputs() is a great deal smaller than the code for fprintf(), which may imply that it executes less instructions and is therefore faster. To confirm this assumption, put fputs() and fprintf() inside loops and then benchmark the start and end times. Thus you can verify that fputs() ends up faster.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Thank you for your help.


    Hello Scorpions4ever. That is an excellent answer.

IMN logo majestic logo threadwatch logo seochat tools logo