What is Declaration by Inference in C?
In C, “Declaration by Inference” is not a standard feature of the language. Unlike more modern languages such as C++ (with auto), Python, or JavaScript, C does not allow the compiler to deduce the type of a variable from its initializer automatically. Instead, C requires explicit type declarations for all variables and functions.
However, some developers use the phrase “declaration by inference” informally to refer to techniques like using macros, typedefs, or pointers to functions, which can hide the real type, making it seem like the type is guessed automatically.
Why Type Inference Doesn’t Exist in C
C was designed in the early 1970s with an emphasis on performance and low-level control. As a result:
- C requires types to be explicitly declared.
- The compiler must know exact memory layouts and operations.
- Allowing type inference would complicate this goal.
So, in standard C (C89/C99/C11/C17), you must specify the data type during declaration.
Workarounds for Missing Type Inference
Although C lacks true type inference, you can simulate a similar effect in a few ways:
1. Using #define Macros
#define VAR int VAR a = 10;
Here, VAR is just a macro that expands to int. This isn’t real inference, but can hide the type name.
2. Using typedef
typedef float real; real pi = 3.14;
typedef can be used to alias complex types.
3. Function Pointers and Arrays of Function Pointers
int (*operation)(int, int); // Pointer to a function
These are advanced but require explicit typing. Developers often misunderstand them as inference due to syntax complexity.
4. Void Pointers (void *)
void *ptr; int num = 5; ptr = #
A void * can point to any data type, but you lose type safety when using it. This isn’t true type inference—it’s just that the type information has been removed.
Misconception: auto in C
While auto is a keyword in C, it does NOT perform type inference.
auto int x = 10; // Same as int x = 10;
In C, auto is a storage class, not a tool for type inference. It simply means the variable has automatic (stack) storage, which is already the default, so using it is usually unnecessary.
Examples
Example 1:
#include <stdio.h> typedef int score; #define VAL 99 int main() { score quiz = VAL; printf("Sanfoundry Score: %d\n", quiz); return 0; }
Output:
Sanfoundry Score: 99
Here, score and VAL hide the actual type and value, but C still needs you to declare types clearly. There’s no real type inference happening — it’s just simple macro replacement.
Example 2:
#include <stdio.h> // Typedef for a function pointer type typedef int (*operationFunc)(int, int); // Function definition int multiply(int x, int y) { return x * y; } int main() { // Looks like inferred type operationFunc quizMultiply = multiply; int result = quizMultiply(6, 7); printf("Sanfoundry Quiz Result: %d\n", result); return 0; }
Output:
Sanfoundry Quiz Result: 42
This C program uses a function pointer with a typedef to keep the code simple. It defines operationFunc as a pointer to a function that takes two integers and returns an integer. The multiply function returns the product of two numbers. In main, quizMultiply is set to point to multiply. It then calls the function with 6 and 7 and prints the result: “Sanfoundry Quiz Result: 42.” Though it looks like type inference, C still needs types defined clearly.
Best Practices
- Always declare your variables with explicit types.
- Avoid using auto in C; it’s confusing and unnecessary.
- Don’t rely on compiler extensions like __auto_type unless you’re writing very low-level or GCC-specific code.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Check Computer Science Books
- Check C Books
- Watch Advanced C Programming Videos
- Apply for C Internship