Thread: Getting the data type of an unknown variable

  1. #1
    Registered User
    Join Date
    Mar 2012

    Getting the data type of an unknown variable


    How can I get the data type of an unknown variable?

    For example, I want to do something similar to:

    int func(int x)
       if (the data type of x is int)
           return 0;
           return 1;
    How can I implement this function in C?

    I have read somewhere that this cannot be done in C, but I was wondering if there is an equivalent way to check the data type of the argument?

    Thank you.

  2. #2
    Registered User
    Join Date
    Dec 2011
    You have to use macros and typeof() for that. Example:
    #define func(x) ((typeof(x) == int) ? 0 : 1)
    Not tested, but something like this should work.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Why do you want to do this?

    In some cases, providing a callback function would be more appropriate.
    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

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by memcpy View Post
    Not tested, but something like this should work.
    It doesn't . Or: typeof() is non-standard, but if you mean the GNU typeof, you can't use it like that. I think it is a macro to start with and what you would end up with is something like "if (int == int)" (or ("if char == int")), which would be an error.

    In any case:

    I have read somewhere that this cannot be done in C
    Right, because it is not logically meaningful:

    int func(int x)
    The type of x is declared as int. x is an int, end of story.

    WRT pointers, they may point to data that is not structured the way they have been cast or used, but unless you do it yourself, that data does not contain any type indicator. Ie, for custom types, you can implement your own thing this way (but simply adding a param to the function would be better).

    Personally, I don't see much use value for this, even in languages which do have a real typeof().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Such a function does not exist because the type is known and does not need checking. It doesn't exist for the same reason that you don't need to ask yourself each morning "am I human?".

    If you have a void* on the other hand, then that's a different story. Then the answer would be to look at the caller and callee, and design a solution that is sensible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2006
    In C++ you can do something like that with templates (with restrictions), but not in C.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Thanks a lot for all these answers. It is clear then that implementing this checking is a waste of time...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  2. unknown file type reading
    By xixonga in forum C Programming
    Replies: 20
    Last Post: 11-28-2010, 12:17 PM
  3. Error-size of te type is unknown
    By as_rule in forum C Programming
    Replies: 5
    Last Post: 08-29-2010, 08:28 AM
  4. unknown size for type 'void'
    By Noobwaker in forum C Programming
    Replies: 12
    Last Post: 04-05-2006, 06:41 AM
  5. Difference between 'data type' and variable?
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 02-02-2006, 08:48 AM