-4

How to know the type of a variable in C ?

I tried sizeof() but i'm not convinced by the answer and i don't know how to use typeof() in C.

Some help ?

If we have for example : int i ; then what is the type of ('A' + i). i though i can do something like printf("%s", typeof('A' + i)); to display the right type of my expression but i don't kwon how work a gcc expression.

Help please !

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
Jean-Paul
  • 127
  • 2
  • 15
  • 2
    there is no reflection in C. – Jason Hu Oct 30 '17 at 19:27
  • 4
    Can you be a bit more specific about what you're trying to do? – David Schwartz Oct 30 '17 at 19:28
  • 2
    C is statically typed, so looking up variable's declaration gives you its exact data type. – Sergey Kalinichenko Oct 30 '17 at 19:28
  • 1
    Like @HuStmpHrrr said, there's no way in C. What is your use case? Maybe there is an alternative solution. – bhow Oct 30 '17 at 19:28
  • Related: https://stackoverflow.com/questions/12081502/typeof-operator-in-c – John Bollinger Oct 30 '17 at 19:28
  • 1
    You do not have the luxury of disbelieving the result of the `sizeof` operator. If your compiler were so badly broken that it did not compute sizes correctly then you pretty much could not rely on it for *anything*. If `sizeof` produces a result that surprises you then it's probably because you have some misunderstanding about the operand. – John Bollinger Oct 30 '17 at 19:32
  • If we have for exemple : int i ; then what is the type of ('A' + i). i though i can do something like printf("%s", typeof('A' + i)); to display the right type of my expression but i don't kwon how work a gcc expression. – Jean-Paul Nov 03 '17 at 19:29

1 Answers1

1

Introspection/reflection and is not supported in C language. However, there is a typeof extension in GCC (not part of ANSI)

Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • If we have for exemple : int i ; then what is the type of ('A' + i). i though i can do something like printf("%s", typeof('A' + i)); to display the right type of my expression but i don't kwon how work a gcc expression. – Jean-Paul Nov 03 '17 at 19:22
  • This is not how it works, the typeof extension basicly allows you to do declare a variable using the type of another variable. It's similar to the auto keyword in C++. – Gnqz Nov 04 '17 at 06:52