Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Quick answer: change <code>int testlib()</code> to <code>int testlib(void)</code> to specify that the function takes no arguments.</p> <p>A <em>prototype</em> is by definition a function declaration that specifies the type(s) of the function's argument(s).</p> <p>A non-prototype function declaration like</p> <pre><code>int foo(); </code></pre> <p>is an old-style declaration that does not specify the number or types of arguments. (Prior to the 1989 ANSI C standard, this was the only kind of function declaration available in the language.) You can call such a function with any arbitrary number of arguments, and the compiler isn't required to complain -- but if the call is inconsistent with the <em>definition</em>, your program has undefined behavior.</p> <p>For a function that takes one or more arguments, you can specify the type of each argument in the declaration:</p> <pre><code>int bar(int x, double y); </code></pre> <p>Functions with no arguments are a special case. Logically, empty parentheses would have been a good way to specify that an argument but that syntax was already in use for old-style function declarations, so the ANSI C committee invented a new syntax using the <code>void</code> keyword:</p> <pre><code>int foo(void); /* foo takes no arguments */ </code></pre> <p>A function <em>definition</em> (which includes code for what the function actually does) also provides a <em>declaration</em>. In your case, you have something similar to:</p> <pre><code>int testlib() { /* code that implements testlib */ } </code></pre> <p>This provides a non-prototype declaration for <code>testlib</code>. As a definition, this tells the compiler that <code>testlib</code> has no parameters, but as a declaration, it only tells the compiler that <code>testlib</code> takes some unspecified but fixed number and type(s) of arguments.</p> <p>If you change <code>()</code> to <code>(void)</code> the declaration becomes a prototype.</p> <p>The advantage of a prototype is that if you accidentally call <code>testlib</code> with one or more arguments, the compiler will diagnose the error.</p> <p>(C++ has slightly different rules. C++ doesn't have old-style function declarations, and empty parentheses specifically mean that a function takes no arguments. C++ supports the <code>(void)</code> syntax for consistency with C. But unless you specifically need your code to compile both as C and as C++, you should probably use the <code>()</code> in C++ and the <code>(void)</code> syntax in C.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload