Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:17:02+00:00 2026-05-26T21:17:02+00:00

Possible Duplicate: How can I simulate OO-style polymorphism in C? I’m trying to better

  • 0

Possible Duplicate:
How can I simulate OO-style polymorphism in C?

I’m trying to better understand the idea of polymorphism with examples from languages I know;
is there polymorphism in C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:17:03+00:00Added an answer on May 26, 2026 at 9:17 pm

    This is Nekuromento’s second example, factored in the way I consider idiomatic for object-oriented C:

    animal.h

    #ifndef ANIMAL_H_
    #define ANIMAL_H_
    
    struct animal
    {
        // make vtable_ a pointer so they can be shared between instances
        // use _ to mark private members
        const struct animal_vtable_ *vtable_;
        const char *name;
    };
    
    struct animal_vtable_
    {
        const char *(*sound)(void);
    };
    
    // wrapper function
    static inline const char *animal_sound(struct animal *animal)
    {
        return animal->vtable_->sound();
    }
    
    // make the vtables arrays so they can be used as pointers
    extern const struct animal_vtable_ CAT[], DOG[];
    
    #endif
    

    cat.c

    #include "animal.h"
    
    static const char *sound(void)
    {
        return "meow!";
    }
    
    const struct animal_vtable_ CAT[] = { { sound } };
    

    dog.c

    #include "animal.h"
    
    static const char *sound(void)
    {
        return "arf!";
    }
    
    const struct animal_vtable_ DOG[] = { { sound } };
    

    main.c

    #include "animal.h"
    #include <stdio.h>
    
    int main(void)
    {
        struct animal kitty = { CAT, "Kitty" };
        struct animal lassie = { DOG, "Lassie" };
    
        printf("%s says %s\n", kitty.name, animal_sound(&kitty));
        printf("%s says %s\n", lassie.name, animal_sound(&lassie));
    
        return 0;
    }
    

    This is an example of runtime polymorphism as that’s when method resolution happens.

    C1x added generic selections, which make compile-time polymorphism via macros possible. The following example is taken from the C1x April draft, section 6.5.1.1 §5:

    #define cbrt(X) _Generic((X), \
        long double: cbrtl, \
        default: cbrt, \
        float: cbrtf \
    )(X)
    

    Type-generic macros for math functions were already available in C99 via the header tgmath.h, but there was no way for users to define their own macros without using compiler extensions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can a number used to name a sql column I am trying
Possible Duplicate: Can I run a .NET garbage collection from WinDbg? I'm debugging a
Possible Duplicate: How can I simulate an anchor click via jquery? For some reason
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Can select * usage ever be justified? Curious to hear this from
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Can i override HOME Button on my own Home-Screen from my application?
Possible Duplicate: Can anyone recommend a simple Java web-app framework? I want to know
Possible Duplicate: Can I access session state from an HTTPModule? I have set up
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.