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.
6
  • 1
    There is a gcc extension 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. Commented Jul 20, 2015 at 6:59
  • I am learning C and Python simultaneously. Since it is available in Python, I thought that it might be available in C too. Commented Jul 20, 2015 at 7: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. Commented Jul 20, 2015 at 7:22
  • Python has some degree of language reflection 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. Commented Jul 20, 2015 at 7:57
  • Answer for your question stackoverflow.com/questions/5127797/… Commented Jul 20, 2015 at 7:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.