Thread: How to find type of variables?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    How to find type of variables?

    Hi guys

    Is there a command in C that returns the type of a variable? E.g. if we declare

    Code:
    int a;
    then is there a command X such that X(a) returns int (or any other variable type, for that matter)?

    Best,
    Niles.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    C is a statically typed language. Mostly what that means is that if something has a particular type, it's because your code explicitly made it that type. You can cast variables to other types, but one of the most powerful features of C is that it's then treated 100% like that type. Every thing is written directly as binary data to memory - there's no meta-data to remember what you intended something to be like. If you cast a char to an int, it's now an int. If you cast it to a void *, it's now generic. And if you're not casting, there's no way to not know what type something should be.

    Depending on what it is you're trying to do, we might be able to make some suggestions as to go about something like this. For instance, if you have a function that take a void * but might treat it differently depending on what type the calling function intended it to be, you could also pass in an enum or something to specify the type, and have conditionals to handle that inside the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 08:50 PM