Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit declaration in C language

Tags:

c

Consider the following quote from the C book by Dennis ritchie

All variables must be declared before use, although certain declarations can be made implicitly by content.

It is known that all variables of any type must be declared before using it further. I am unaware with the latter part of the statement that certain declarations can be made implicitly by content.

In C, in general, the variables fall under four basic data types char, int, float, double. How can a variable from these datatypes can be used without any declaration before. Please provide an example that shows implicit declaration based on content the variable holds.

like image 901
hanugm Avatar asked May 28 '17 10:05

hanugm


2 Answers

By "certain declarations" the author means declaration of things which are not variables. At the time the book has been written C allowed implicit declaration of functions: the compiler simply assumed that the function returns integer. Modern C standards make such declarations illegal.

like image 74
Sergey Kalinichenko Avatar answered Oct 20 '22 05:10

Sergey Kalinichenko


When the first edition of K&R was written, there was no C standard. When the second edition of K&R was written, the C89/C90 standard was about to be finalized. Because of the legacy from code written before C89 was finalized, the standard had to permit:

#include <stdio.h>

double sqrt();

main(argc, argv)
    char **argv;
{
    if (argc > 1)
        printf("sqrt(%s) = %f\n", argv[1], sqrt((double)atoi(argv[1])));
    else
        printf("sqrt(%.0f) = %f\n", 2.0, sqrt(2.0));
    return 0;
}

Note that the return type of main() is implicitly int; the function argument argc is implicitly int; the function atoi() has an implicit return type of int. Note too that the argument to sqrt() had to be explicitly a double value; the compiler could not automatically convert the argument type because prototypes were not a part of C before the C89 standard.

Such code is no longer acceptable to C99 or C11 compilers. You could use:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    if (argc > 1)
        printf("sqrt(%s) = %f\n", argv[1], sqrt(atoi(argv[1])));
    else
        printf("sqrt(%.0f) = %f\n", 2.0, sqrt(2));
    return 0;
}

This uses the standard headers to declare the functions with complete prototypes, so it is no longer necessary to cast the argument to sqrt(). In C99 or C11, you could omit the return 0; and the effect would be the same. Personally, I don't like the loophole that allows that and continue to write the return explicitly. The return was necessary in C90 to send a determinate status to the environment (e.g. the shell the invoked the program).

like image 21
Jonathan Leffler Avatar answered Oct 20 '22 04:10

Jonathan Leffler