2

I'm a novice in C, and since C does some implict changing at times, I often get confused. I'm getting confused in what type(like int,char) does the operation(+,-) returns. So in C, I want to know what type a variable belongs to at any point in a program. That is in Java we call it as Reflection and we can get all the information of our programs at runtime.

Now in C is there any lib that does the similar job of Reflection API in java. Or there is any trick in C, that can be used to find what type does a particular variable belongs to?

Any idea? Thanks in advance.

Mark B
  • 95,107
  • 10
  • 109
  • 188
Ant's
  • 13,545
  • 27
  • 98
  • 148
  • You need to get that information at **Run-Time**? – Alok Save Sep 13 '11 at 16:04
  • Are you asking specifically a C question? Should remove the C++ tag otherwise you may get answers that are C++-specific. – wkl Sep 13 '11 at 16:06
  • 1
    I removed the C++ tag since this is about C. – Mark B Sep 13 '11 at 16:06
  • Various duplicates and near duplicates from the "related" sidebar: http://stackoverflow.com/questions/1353022/reflection-support-in-c http://stackoverflow.com/questions/4497328/how-to-do-reflection-in-c http://stackoverflow.com/questions/6312493/does-c-already-have-some-kind-of-reflection – dmckee --- ex-moderator kitten Sep 13 '11 at 16:07
  • 2
    cheap trick: `typedef struct {} X;` and then `X x = expression`; and then read the error message ;) – etarion Sep 13 '11 at 16:09
  • It might be worth elaborating **why** you would have need for such a thing. Usually the answer points suggests that your design isn't quite idiomatic C and could be improved. – Kerrek SB Sep 13 '11 at 16:11
  • 1
    @Ant's: I didn't downvote or closevote this, but if you're looking for an explanation: I imagine that it's because people feel that you're asking this question only because of your lack of knowledge of C, and once you know C a bit better, you wouldn't even ask this question. Therefore it's considered a question of low value to others. Don't let that discourage you! Keep on learning, and do feel free to come back for further questions. – Kerrek SB Sep 13 '11 at 16:32
  • 1
    Let me get this straight: People downvote because the guy who writes the question doesn't know the answer? That seems outright... arrogant. – Ira Baxter Sep 13 '11 at 16:36

3 Answers3

3

C and C++ are statically typed languages, so there is no reflection and no library for type discovery. In C, you just have to read the standard and understand the type promotion rules. Luckily, that's a finite amount of information that you should be able to grasp quickly.

In the new C++11, there's the decltype keyword which returns the type of the expression, so you can say decltype(x + y) z = x + y; to declare z to be of the type of the expression x + y. This is a compile-time construction, though, so this is merely a shortcut to something you could have inferred by other means.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Uh, Java is statically typed and has reflection. Whether a language has "reflection" built in is a matter what the language and compiler guys decided would be available for introspective queries at runtime. – Ira Baxter Sep 13 '11 at 16:32
  • @Ira: I would really hesitate to call Java "statically typed". I mean, yes, for ints and doubles, fair enough. But look at just any container, for example - Java classes basically consist exclusively of all-virtual functions and what C++ would call `dynamic_cast`s. – Kerrek SB Sep 13 '11 at 16:34
  • Statically typed means in principle that every entity has a known type at compile time, and generally that the compiler checks that what is used has a type compatible (for some definition of "compatible") with the place it is used. In Java, most methods don't accept just Object; they often accept a detailed specialization of it. I wouldn't hesitate for a minute to call Java statically typed. Neither does the rest of the world: http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B – Ira Baxter Sep 13 '11 at 16:45
  • @Ira: Out of curiosity: Does Java's reflection work for primitive (value) types like `int` and `double`? – Kerrek SB Sep 13 '11 at 16:51
0

If you use #include <typeinfo> you can then use typeid to get the variable type.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
0

If your implementation supports C++ ABI, you can use it to print out expression types.

#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <cxxabi.h>

int main ()
{
    int status;
    char* mytypename = abi::__cxa_demangle(typeid((2+'x')*0.9f).name(), 0, 0, 
                                             &status);
    if (mytypename && status == 0)
    {
        std::cout << mytypename << std::endl;
        std::free (mytypename);
    }
    else
    {
        std::cerr << "Error determining type name, status is " 
                   << status << std::endl;
    }
}

Such things are useful mostly for educational purposes. There's not much you can do with them, apart from looking and learning.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243