1

Is typeof operator available in C. If yes how to implement it. Or is there a header file I must use? I am getting the following error when I try to use the typeof statement

  • Undeclared function 'typeof' (did you mean feof?); assuming extern returning int.
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Karthik S
  • 106
  • 1
  • 6
  • 1
    There is a [gcc extension](https://gcc.gnu.org/onlinedocs/gcc/Typeof.html) available, but it doesn't do any runtime magic like you might expect since C doesn't really have typed runtime polymorphism. What are you trying to do with it? If you're trying to work out what type something was before it was cast, you have to keep track of that kind of information yourself. – Iskar Jarak Jul 20 '15 at 06:59
  • I am learning C and Python simultaneously. Since it is available in Python, I thought that it might be available in C too. – Karthik S Jul 20 '15 at 07:03
  • 2
    Python is a dynamically typed, interpreted language. C is neither. Python has classes. C does not. In Python, you can use `typeof` to find the type of a variable at runtime. In C, you know the types of your variables at compile time because you have to explicitly declare them in the source. E.g. `void foo(int x)` in C vs `def foo(x):` in Python. – Iskar Jarak Jul 20 '15 at 07:22
  • Python has some degree of [language reflection](https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29) that C does not. Reflection is a general concept that is quite useful to know about, and will help you quickly glance which languages support this kind of feature and which do not. – anol Jul 20 '15 at 07:57
  • Answer for your question http://stackoverflow.com/questions/5127797/could-i-retrieve-the-datatype-from-a-variable-in-c – Sorcrer Jul 20 '15 at 07:57
  • possible duplicate of [typeof operator in C](http://stackoverflow.com/questions/12081502/typeof-operator-in-c) – Jaffer Wilson Jul 20 '15 at 08:10

1 Answers1

2

C is a much lower level language than Python with very little magic. It is good for low level tasks - the reference implementation of Python is written in C ...

Even if recent C version are less tolerant than good old K&R C from the 1970s (*), the rule is mainly : if the programmer knows it, there's no use the compiler care's for it. Examples :

  • arrays : the programmer shall care for the size, the compiler only cares for the starting address
  • variable type : the programmer should know, compiler only cares for it at compile time, but does not store information for run time

(*) there are indeed rules enforced by the compiler, but mainly at compile time : at run time the program is stripped down to a minimum

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252