-2

Here's a code:

#include <stdio.h>

/* declaration */
int square (int num);

int main() {
    int x, result;
  
    x = 5;
    result = square(x);
    printf("%d squared is %d\n", x, result);
    
    return 0;
}

/* definition */
int square (int num) {
    int y;

    y = num * num;

    return(y);
}

And when I change it like this:

#include <stdio.h>
/* declaration */

int main() {
    int x, result;
  
    x = 5;
    result = square(x);
    printf("%d squared is %d\n", x, result);
    
    return 0;
}

/* definition */
int square (int num);
int square (int num) {
    int y;

    y = num * num;

    return(y);
}

I get the warning:

Implicit declaration of the function 'square'

Does this warning has any relation with the branch prediction things? Does the former code improves branch prediction and hence the performance of the code (as the function call branches/diverts the execution from the normal execution of the code!?)

lousycoder
  • 451
  • 6
  • 10
  • 1
    Does this answer your question? [What does "implicit declaration of function" mean?](https://stackoverflow.com/questions/2161304/what-does-implicit-declaration-of-function-mean) – user202729 Feb 22 '21 at 05:00
  • You need to move the square prototype above the main declaration – SuperStormer Feb 22 '21 at 05:04
  • Don't worry about branch prediction for now, it's an advanced topic. Study the basics first, such as how to use functions. – Lundin Feb 22 '21 at 08:35
  • @user202729 The thing is, I know how to avoid this warning/error by following the standard way of declaring the function. I want to know and possibly change why I have to do it this way. Compilers are a black box thing. – lousycoder Feb 22 '21 at 12:18
  • Black box? You could search for what the error message mean and what it affects without too much difficulty. (just copy paste the error message into DuckDuckGo/some search engines) The explanation of the answers to the other question is pretty good... (oh no, it isn't that good. Perhaps [Implicit function declarations in C - Stack Overflow](https://stackoverflow.com/questions/9182763/implicit-function-declarations-in-c) is better) – user202729 Feb 22 '21 at 13:27

1 Answers1

3

No, it has nothing to do with branch prediction.

The language requires that a declaration of a function appear before the function is called. The compiler needs to see the declaration in order to generate code for the function call that is even correct, let alone performant: to know what types are expected as parameters, what type is returned, how the parameters should be laid out in the stack and registers, and so on. And by the one-pass design of the C language, this means the declaration has to appear before the function call. It's true that the necessary information is further down in the source file, but that's too late - the compiler isn't designed to "peek ahead" for it.

In modern C, the absence of the declaration makes the program invalid, and the compiler must issue a diagnostic. Previous versions of the C language allowed such a program to compile, using an "implicit declaration" which was essentially a standard set of rules to guess what types were expected and returned, and how they should be passed. Some compilers, such as gcc, proceed under these earlier rules when they find a missing declaration, and only give a warning instead of an error (which complies with the requirement for a diagnostic). But as the programmer, you really should consider it an error, since there's a good chance that the "guessed" implicit declaration will be wrong for your function, in which case the compiled program may fail in unpredictable ways at runtime.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Modern versions of GCC (meaning 5.x or later) assume C11 semantics unless you say otherwise; earlier versions (meaning 4.x or earlier) assume C90, with the considerably sloppier semantics that were necessary to allow the standard to succeed given the existing code base at the time when C90 was promulgated. – Jonathan Leffler Feb 22 '21 at 06:17
  • Thank you for the answer, at least it throws some light on why it can't be done. – lousycoder Feb 22 '21 at 12:22