Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The function is written in K&amp;R style, and your prototype for it is incorrect. In fact, there are other problems too...</p> <pre><code>#include &lt;stdio.h&gt; void main() { extern int fun(float); int a=fun(3.5); printf("%d",a); } int fun(aa) float aa; { return ((int)aa); } </code></pre> <p>The return type of <code>main()</code> is <code>int</code>, at least in Standard C. Your print statement should include a newline.</p> <p>Your prototype would be OK if the function <code>fun()</code> were written with a prototype:</p> <pre><code>int fun(float aa) { ... } </code></pre> <p>However, the function is written in K&amp;R style, and therefore the function expects to be passed a <code>double</code> which it will convert to <code>float</code>:</p> <pre><code>int fun(double aa_arg) { float aa = aa_arg; ... } </code></pre> <p>In K&amp;R C, all <code>float</code> values were passed as <code>double</code>. This is why you are getting garbage; you are lying (probably unwittingly) to your compiler, and it is getting its own back by doing GIGO on you.</p> <p>FWIW: GCC 4.6.1 refuses to compile your code (even without any warning settings). It complains:</p> <pre><code>f1.c: In function ‘main’: f1.c:2: warning: return type of ‘main’ is not ‘int’ f1.c: At top level: f1.c:9: error: conflicting types for ‘fun’ f1.c:3: error: previous declaration of ‘fun’ was here </code></pre> <p>You can fix this in a number of different ways:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { extern int fun(float); int a = fun(3.5); printf("%d\n", a); return(0); } int fun(float aa) { return ((int)aa); } </code></pre> <p>Or:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { extern int fun(double); int a = fun(3.5); printf("%d\n", a); return(0); } int fun(double aa) { return ((int)aa); } </code></pre> <p>Or:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { extern int fun(double); int a = fun(3.5); printf("%d\n", a); return(0); } int fun(aa) double aa; { return ((int)aa); } </code></pre> <p>Or:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { extern int fun(double); int a = fun(3.5); printf("%d\n", a); return(0); } int fun(aa) float aa; { return ((int)aa); } </code></pre> <p>Or:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { extern int fun(); int a = fun(3.5); printf("%d\n", a); return(0); } int fun(aa) float aa; { return ((int)aa); } </code></pre> <p>I believe these are all correct and they all should compile without warnings (unless you ask the compiler to complain about old style (K&amp;R) function definitions, etc). </p> <p>With GCC set to rather fussy, I get the warnings:</p> <pre><code>/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f2.c -o f2 f2.c:12: warning: no previous prototype for ‘fun’ /usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f3.c -o f3 f3.c:12: warning: no previous prototype for ‘fun’ /usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f4.c -o f4 f4.c:12: warning: function declaration isn’t a prototype f4.c: In function ‘fun’: f4.c:13: warning: old-style function definition /usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f5.c -o f5 f5.c:12: warning: function declaration isn’t a prototype f5.c: In function ‘fun’: f5.c:13: warning: old-style function definition /usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f6.c -o f6 f6.c: In function ‘main’: f6.c:5: warning: function declaration isn’t a prototype f6.c: At top level: f6.c:12: warning: function declaration isn’t a prototype f6.c: In function ‘fun’: f6.c:13: warning: old-style function definition </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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