0

I was implementing linked list in C program worked but gave the following warnings in terminal

linkedstr.c:36:11: warning: a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
    link *search_link() , *pred_link();
          ^
linkedstr.c:36:28: warning: a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
    link *search_link() , *pred_link();

here is the code of the following functions

link *search_link(link *l, int x)
{
    if (l == NULL)
        return NULL; //end of list is reached
    if (l->val == x)
        return l;
    else
        return (search_link(l->next, x)); //search sub_list
}

link *pred_link(link *l, int x)
{
    if (l == NULL || l->next == NULL)
        return (NULL);
    if ((l->next)->val == x)
        return (l);
    else
        return (pred_link(l->next, x));
}

I googled and there was an explanation link] but it says that we can't leave parameters empty and we foo(void) must be passed instead of foo() but I clearly gave the parameters in my functions so why the warnings?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
gunslinger
  • 43
  • 4
  • It looks like another line of code (`linkedstr.c:36`) is trying to call these two functions, but doesn't pass them arguments. – GandhiGandhi Aug 25 '23 at 17:52
  • Your problem is elsewhere please include the code from the error – UpAndAdam Aug 25 '23 at 17:55
  • Wjhatever resource you're using to learn C it seems to be quite old and outdated. – Some programmer dude Aug 25 '23 at 17:57
  • In C18 or earlier, the line `link *search_link() , *pred_link();` says "there are two functions, `search_link()` and `pred_link()` which both return a `link *` but nothing is known about what parameters they accept". In C23, that same line will say, "the functions take no parameters at all". This is a major change of meaning; it's how a C++ compiler would interpret those declarations, too. Always declare functions with a prototype giving the types of the parameters they take, using `(void)` if they don't take any. Ensure the function prototype is declared before calling a function (C99 on). – Jonathan Leffler Aug 25 '23 at 18:21
  • The diagnostic message tells you the line number and even the position on the line of the code about which it is warning. It even presents the offending code itself. Clearly the code to which the diagnostic applies is *not* the code presented for our consideration. – John Bollinger Aug 25 '23 at 19:33

2 Answers2

3

The link talks about "function declaration without a prototype", which are these

 link *search_link() , *pred_link();

Once upon a time you could leave () empty, and the compiler would have to kind of guess what parameters the functions use.

This has been "bad" for a very long time, and will soon be forbidden in C23 (like the compiler says). So just add the parameter types there too, to make them prototypes.

BoP
  • 2,310
  • 1
  • 15
  • 24
3

To understand the compiler message, you need to know the definition of prototype. C 2018 6.2.1 2 says:

  • … (A function prototype is a declaration of a function that declares the types of its parameters.)

So, when the compiler message says “a function declaration without a prototype,” it is referring to these declarations:

link *search_link() , *pred_link();

link *search_link() declares a function but does not declare the types of its parameters. So it is a function declaration without a prototype.

To remedy this, add the parameter types to the declarations:

link *search_link(link *, int), pred_link(link *, int);

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312