Thread: Question regarding library functions/macros

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Question regarding library functions/macros

    Hi everyone, I am currently learning C using KNKing C prrogramming a modern approach.

    I am currently learning about the functions in the stdio.h library. There are some parts where it talks about how some functions are usually implemented as a macro while others are implemented only as a function?

    E.g. this part about input functions fgetc, getc, getchar and ungetc.

    "The relationship between getc and fgetc is similar to that between putc and fputc. getc is usually implemented as a macro (as well as a function) while fgetc is implemented only as a function. getchar is normally a macro as well:

    #define getchar() getc(stdin) "

    I have some questions: What does it mean by functions are implemented as macros while sometimes implemented as functions?

    also how can getchar, which is a function in the stdlib library, be define as a macro? Thank you all.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    There are a kind of macros called function-style macros that allow for macros that look like functions. Being a macro, you get the same simplistic text replacement of macros, hence avoiding function call overhead, and being function-style, you can pass arguments to the macro, except that those arguments aren't evaluated but rather the text replacement mechanism applies.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    I see. So some "commands" in the library can be both macros AND functions (and if used they are macros, unless i add "()" to them to force the machine to compile it as a function), while some "commands" are only functions? Thank you sir.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    So some "commands" in the library can be both macros AND functions
    I didn't say that. They can be macros or functions, but not both at the same time.

    (and if used they are macros, unless i add "()" to them to force the machine to compile it as a function),
    I didn't say that at all either. I talked about function-style macros, not about macros that become functions when you add parentheses.

    Function-style macros are macros, not functions. The idea is that the standard allows some functions to be implemented as macros instead. If the implementation chooses this option, then they are macros, not functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    There is a long history here....

    Back in the old days optimizing compilers (as we know them now) didn't exist. The compiler was very much a "Garbage In, Garbage Out" thing. Memory was much smaller than today, and CPU cycles much more more expensive. I still remember using Lattice C, which required 96KB or RAM and two floppy disks...

    Design decisions were made. One of those was that for really trivial functions they would be implemented as macros. The reason for this was that the time taken to setup and call the function and return would be a significant proportion of the execution time, and that overweighed other concerns like code size.

    One easy to talk about example could be "int isdigit(int c)":

    Code:
    int isdigit(int c) {
       return (c >= '0' && c <= '9');
    }
    To save resources, and speed up the program it could be implemented as a macro:

    Code:
    #define isdigit(c) (c >= '0' && c <= '9')
    This could be the logical choice at the time, but if implemented this way it introduces bugs. For example

    Code:
       i += isdigit(getchar());
    would become:

    Code:
       i += (getchar() >= '0' && getchar() <= '9');
    And this calls getchar() twice.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by laserlight View Post
    They can be macros or functions, but not both at the same time.
    They can be both at the same time.
    You can ensure you call the function by putting parens around the function name in the call.

    Code:
    #include <stdio.h>
     
    void abc(int x) {
        printf("func: %d\n", x);
    }
     
    #define abc(x) printf("%d\n", x)
     
    int main() {
        abc(123);   // calls the macro
        (abc)(123); // calls the function
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    I see. Thank you all for the reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What are macros and why build your own c library?
    By Sankait Laroiya in forum C Programming
    Replies: 12
    Last Post: 09-25-2014, 09:31 AM
  2. parameterized macros vs. functions
    By Tbrez in forum C Programming
    Replies: 3
    Last Post: 04-02-2009, 12:33 PM
  3. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  4. Macros as functions
    By Darklighter in forum Windows Programming
    Replies: 4
    Last Post: 11-25-2006, 08:56 AM
  5. Macros Vs Inline functions
    By passionate_guy in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:52 AM

Tags for this Thread