What is Declaration by Inference in C?

Question: What is Declaration by Inference in C

Answer: Let’s try some examples of declaration of variables in C,

    int  x;
    int *y;

Observe that x is declared to be an integer. What about *y? Expression *y is also an integer. We deduce from here that y is a pointer to integer. This assertion can be validated in C as,

    int*    x, y;

What are x and y, now? Let’s try them in a simple C program,

#include <stdio.h>
 
int main(void)
{
    int*    x = 10, y;
 
    return 0;
}

What happens when you compile this program, it turns out with a warning,

advertisement
advertisement
warning: initialization makes pointer from integer without a cast 
[enabled by default]

Notice here that despite the spacing, asterisk(*) is associated with x making it a pointer to int while y is an ordinary integer.

Try one more example,

    int xyz(int, int);

xyz is a function which takes two integer arguments and returns an int.

Remember that rules for declaration by inference for reading declarations are the same as those for ordinary expressions.

We can try several other expressions using declaration by inference, as,

    int     (*f)(int, int);
    int     (*x[5])(char, char);

Well! If declaration by inference seems to be a confusing for complex declarations, you can apply the same rules for evaluation of declarations as there are for other expressions in C. There are no separate rules or separate syntax. Let’s figure out the above two complex declarations below,

advertisement
    int     (*f)(int, int);
    int     (*x[5])(char, char);

Notice that in first declaration, we have two sets of parenthesis of which first one being grouping has higher precedence making f a pointer to function that takes two integers as arguments and returns an integer.

In second declaration, again grouping goes first, making x an array of pointers, then second set of parenthesis, a function call, evaluates and results x an array of pointers to functions which take two char type values and return int.

You can this way evaluate any complex declaration. Fortunately, there are programs called cdecl for c declarations and c++decl for c++ declarations, which don’t come pre-installed on Linux Machines, which convert between English and declarations. Below given link, you can use to download these programs.

advertisement
    http://www.ibiblio.org/pub/Linux/devel/lang/c/

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.