I downloaded this source code (.zip) and would like to compile it on my Mac (OSX 10.11.2) with an XCode Version 7.2 (7C68).
I started to compile the file fdist.c with
gcc -o fdist2 -O fdist.c -lm
but it returns a long series of warnings and errors (see below). Looking at the file I see that indeed it does not quite look as the kind of code I am used to. Typically, it appears that the type of object the functions returned are unspecified.
The README_fdist2 did not help much. There are guidelines on how to compile the code but there is a typo in the first line.
How should I compile this code?
Below are the errors and warnings the command gcc -o fdist2 -O fdist.c -lm returned
fdist2.c:42:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
fdist2.c:117:3: warning: implicit declaration of function 'sim1' is invalid in C99
[-Wimplicit-function-declaration]
sim1(init,initot,rm,mu,freq_arr,val_arr,&noall);
^
fdist2.c:119:4: warning: implicit declaration of function 'my_thetacal' is invalid in C99
[-Wimplicit-function-declaration]
my_thetacal(freq_arr,noall,init,Subs,&h0,&h1,&fst);
^
fdist2.c:136:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
sim1(init,initot,rm,mu,freq_arr,val_arr,noall)
^
fdist2.c:222:3: warning: implicit declaration of function 'dfill' is invalid in C99
[-Wimplicit-function-declaration]
dfill();
^
fdist2.c:234:4: warning: implicit declaration of function 'cnode' is invalid in C99
[-Wimplicit-function-declaration]
cnode(k);
^
fdist2.c:237:8: warning: implicit declaration of function 'mnode' is invalid in C99
[-Wimplicit-function-declaration]
else mnode(k);
^
fdist2.c:252:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
fdist2.c:254:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
thetacal(gen,noall,sample_size,no_of_samples,het0,het1,fst)
^
fdist2.c:293:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
fdist2.c:301:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
dfill()
^
fdist2.c:312:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
fdist2.c:315:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
cnode(sp)
^
fdist2.c:349:2: error: non-void function 'cnode' should return a value [-Wreturn-type]
return;
^
fdist2.c:353:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
mnode(sp)
^
fdist2.c:464:2: error: non-void function 'mnode' should return a value [-Wreturn-type]
return;
^
fdist2.c:489:10: warning: implicit declaration of function 'poidev' is invalid in C99
[-Wimplicit-function-declaration]
mutno = poidev(time*mu);
^
fdist2.c:491:12: warning: implicit declaration of function 'addmut' is invalid in C99
[-Wimplicit-function-declaration]
p->dna = addmut(p->dna);
^
fdist2.c:676:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
mom(x,n,x1,x2,x3,x4,min,max)
^
fdist2.c:707:2: error: non-void function 'mom' should return a value [-Wreturn-type]
return;
^
fdist2.c:761:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
my_thetacal(gen,noall,sample_size,no_of_samples,het0,het1,fst)
^
18 warnings and 3 errors generated.
The problem is not the compiler but the fact that your code does not follow the syntax. C99 requires that all variables and functions be declared ahead of time. Function and class definitions should be placed in a .h header file and then included in the .c source file in which they are referenced.
i.e.
mycode.h:
int myFunction (int a, char * s);
mycode.c
#include "mycode.h"
int main(int argc, char **argv) {
int x = 2;
char * str = "Hello";
int r = myFunction(x,str);
return r;
}
Looks like the code doesn't conform to C99 with implicit function declrations, default return (int) etc.
It doesn't seem to be hard to fix it. But if you don't want to or can't then you can attempt to compile in C89/C90 in which implicit int return is valid. This should fix most, if not all, of the warnings you get.
gcc -std=c89 -o fdist2 -O fdist.c -lm
Related
So I am a beginner in C and I have been following a book Primer Plus 6th edition.
While I was running my program I noticed something really strange, compiler is not working as it is supposed to, for example
int main(){
one_three();
getchar();
return 0;
}
void one_three(void){
printf("one\n");
two();
printf("three");
}
void two(void){
printf("two\n");
}
this is my code, there are many errors as no function prototype declarations and not including header files, but some how the output is produced only by giving warnings, i dont think this should happen
NOTE I HAVE INTENTIONALLY REMOVED FUNCTION PROTOTYPE DECLARTAIONS AND INCLUDING HEADER FILES
output is :
review1.c: In function 'main':
review1.c:2:5: warning: implicit declaration of function 'one_three' [-Wimplicit-function-declaration]
2 | one_three();
| ^~~~~~~~~
review1.c:3:5: warning: implicit declaration of function 'getchar' [-Wimplicit-function-declaration]
3 | getchar();
| ^~~~~~~
review1.c:1:1: note: 'getchar' is defined in header '<stdio.h>'; did you forget to '#include <stdio.h>'?
+++ |+#include <stdio.h>
1 | int main(){
review1.c: At top level:
review1.c:7:6: warning: conflicting types for 'one_three'
7 | void one_three(void){
| ^~~~~~~~~
review1.c:2:5: note: previous implicit declaration of 'one_three' was here
2 | one_three();
| ^~~~~~~~~
review1.c: In function 'one_three':
review1.c:8:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
8 | printf("one\n");
| ^~~~~~
review1.c:8:5: warning: incompatible implicit declaration of built-in function 'printf'
review1.c:8:5: note: include '<stdio.h>' or provide a declaration of 'printf'
review1.c:9:5: warning: implicit declaration of function 'two' [-Wimplicit-function-declaration]
9 | two();
| ^~~
review1.c: At top level:
review1.c:13:6: warning: conflicting types for 'two'
13 | void two(void){
| ^~~
review1.c:9:5: note: previous implicit declaration of 'two' was here
9 | two();
| ^~~
review1.c: In function 'two':
review1.c:14:5: warning: incompatible implicit declaration of built-in function 'printf'
14 | printf("two\n");
| ^~~~~~
review1.c:14:5: note: include '<stdio.h>' or provide a declaration of 'printf'
one
two
three
those last three lines "one" two three is the output
Please help, i am using this compiler
I expect it to output errors when it has to and not add stuff itself, so that i could learn by my mistakes
using visual studio code with extensions : C/C++ by microsoft, Code runner
Earlier pre-standard versions of C had the concept of a default type given to objects if they weren't explicitly given a type. Functions, if not declared, were implicitly typed as:
int func();
So a function without a prototype was assumed to return int and take an unspecified number of arguments. Because old code was written with this in mind, many compilers are backward compatible and compile old code such as this, but issues warnings when these old constructs are used.
In your particular case, you're able to get away with the implicit function declarations of the functions you define because the functions have void return type and no attempt is made to use the return value of these functions. For the getchar and printf functions, their return type is int so they can work with the implicit declaration.
You need to declare methods that you are using in functions above the functions you are using them in. If you reorder the functions to be two, then one_three, then main. It should work.
Here's my code:
int main(){
printf("Hi");
int i=10;
printf("Hi %d",i);
return 0;
}
Now as C can implicitly declare a function this program will compile correctly (As it does with gcc).
But my question is, wasn't the 1st printf declared to return an int with 1 parameter of type char * ?
That makes the 2nd printf an error.
Yet the program compiles with no errors, just warnings (gcc). Why ?
Strictly speaking, implicit declaration is standard violation. It is removed from the standard.
Quoting C11, Foreword
Major changes in the second edition included:
— remove implicit function declaration
That said, in earlier version of C, for a function which is considered declared implicitly (i.e., used before the compiler has knowledge about the function prototype) was supposed to
return an int
accepts any number and type of parameter.
So, as long as the function declaration and the definition does not collide (say, return type mismatch), you'll not get any error. However, a strictly conforming compiler MUST produce a diagnostic.
In your case printf is implicitly defined to be int printf(...), not int printf(char *), thus compiler detects no errors when you call it with different arguments.
Your code is not portable C since you're missing the requisite #include which brings in the function prototype for printf: any sort of implicit declaration was removed in C99.
If you want to write non-portable C, then the best thing you can do is to consult your compiler documentation. In this instance it looks like your friendly compiler is defaulting to int printf(...).
gcc unfortunately has some built in notion of printf even though a header file has not been used.
unsigned int fun ( unsigned int x )
{
printf("%s\n");
printf("%u\n",x);
more_fun("%s\n");
more_fun("%u\n",x);
return(x+1);
}
so.c: In function ‘fun’:
so.c:5:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("%s\n");
^
so.c:5:5: warning: incompatible implicit declaration of built-in function ‘printf’
so.c:5:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
so.c:5:12: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
printf("%s\n");
^
so.c:7:5: warning: implicit declaration of function ‘more_fun’ [-Wimplicit-function-declaration]
more_fun("%s\n");
^
fortunately if we make our own it does forget at least a little.
void printf(char *, unsigned int );
void more_fun(char *, unsigned int );
unsigned int fun ( unsigned int x )
{
printf("%s\n");
printf("%u\n",x);
more_fun("%s\n");
more_fun("%u\n",x);
return(x+1);
}
so.c:3:6: warning: conflicting types for built-in function ‘printf’
void printf(char *, unsigned int );
^
so.c: In function ‘fun’:
so.c:7:5: error: too few arguments to function ‘printf’
printf("%s\n");
^
so.c:3:6: note: declared here
void printf(char *, unsigned int );
^
so.c:9:5: error: too few arguments to function ‘more_fun’
more_fun("%s\n");
^
so.c:4:6: note: declared here
void more_fun(char *, unsigned int );
^
but we are talking about one compiler, one compiler does not cover it you have to try many/all. Standard or not a compiler could still notice your changing the use of the function and let you know about it, this compiler chooses not to. The error here is not that the compiler didnt notice the function being used differently from one undeclared instance to another but that there was no declaration, and then it does notice the difference as one would hope.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need someone to tell me what I am doing wrong over here.
void main(void) {
int *arr,n,i;
printf("Enter the number of elements you want to enter=");
scanf("%d",&n);
arr = (int*)calloc(n,sizeof(int));
if(!arr)
return;
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
free(arr);
}
This is nothing but a simple program to input the number of items in an array then take the input and print the inputted values. But whenever I go to input the values in the array after taking the number of items from the user, my program CRASHES. I don't understand what I am doing wrong over here. Need some help immediately. Please run the program and see if it also crashes on other systems. I executed the program using an online compiler and strangely there it was running fine. I am using the Visual Studio 2013 command line compiler to execute my program.
Compiling this piece of code with gcc I get:
calloc.c: In function ‘main’:
calloc.c:4:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
calloc.c:5:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
calloc.c:7:17: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default]
calloc.c:18:5: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
using clang:
calloc.c:1:1: error: 'main' must return 'int'
void main(void) {
^
calloc.c:4:5: warning: implicitly declaring C library function 'printf' with type 'int (const char *, ...)'
printf("Enter the number of elements you want to enter=");
^
calloc.c:4:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
calloc.c:5:5: warning: implicitly declaring C library function 'scanf' with type 'int (const char *, ...)'
scanf("%d",&n);
^
calloc.c:5:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'scanf'
calloc.c:7:17: warning: implicitly declaring C library function 'calloc' with type 'void *(unsigned long, unsigned long)'
arr = (int*)calloc(n,sizeof(int));
^
calloc.c:7:17: note: please include the header <stdlib.h> or explicitly provide a declaration for 'calloc'
calloc.c:18:5: warning: implicit declaration of function 'free' is invalid in C99 [-Wimplicit-function-declaration]
free(arr);
^
4 warnings and 1 error generated.
As clang kindly pointed out, you need to add
#include <stdlib.h>
#include <stdio.h>
to the top of your code, as you are using functions that are declared within them. The functions you are using are in the standard C library, so the code will still compile but you should expect the unexpected. The reason your program crashes is likely to be that the compiler has made an incorrect assumption about the return type of calloc, as it doesn't have the correct declaration. Many systems will let you get away with this but at the very least they should warn you.
On a side note, please see this discussion on casting the return value of malloc (don't cast it). This advice also applies to calloc.
edit
Here's a minimal example which might shed some light on the issue:
a.c
#include <stdio.h>
int main()
{
printf("%f\n", f());
return 0;
}
b.c
float f()
{
return 4.2;
}
These two files can be compiled separately into object files:
gcc -c a.c # creates a.o
gcc -c b.c # creates b.o
No warnings or errors are generated. Switching to C99 mode causes an "implicit function declaration" warning. Note that at compile-time, the function definition isn't needed. These two object files can be linked:
gcc a.o b.o
Now, the linker finds the definitions of the functions so there is no error but it is already too late. The implicit declaration of f() when a.c was compiled causes the output to be incorrect when the program is run (I get 0.000000).
The same thing has happened here as in your question. The function calloc can be found, so there is no error at link-time. However, an incorrect assumption is made about the type that it returns, so it doesn't behave as expected on your system.
I know alot of similar questions were asked before but i couldn't find something that would fix this warning i get:
MyIntFunctions.c:19:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
Occurs here:
void IntPrint (const void *key)
{
printf("%d", *(int*)key); // line 19
printf("\t-->\t");
}
and a similar warning:
MyStringFunctions.c:22:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
void StringPrint (const void *key)
{
printf("%s",(char*)key); //line 22
printf("\t-->\t");
}
I really want to understand what is wrong so i won't do that again in the future.
You need to include the appropriate header
#include <stdio.h>
If you're not sure which header a standard function is defined in, the function's man page will state this.
You need to include a declaration of the printf() function.
#include <stdio.h>
the warning or error of kind IMPLICIT DECLARATION is that the compiler is expecting a Function Declaration/Prototype..
It might either be a header file or your own function Declaration..
warning: incompatible implicit declaration of built-in function 'printf'
warning: incompatible implicit declaration of built-in function 'scanf'
the above warnings of compiler says that there is need to include declaration of printf and scanf i.e. include appropriate header
#include <stdio.h>
This is a basic C code, which according to me should have thrown three errors (function not defined, function not returning anything, function argument missing). But to my surprise it threw nothing, it compiled and gave some garbage results:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a=f1();
printf("a %d\n",a);
system("PAUSE");
return 0;
}
f1(int *t)
{
printf("t %d", t);
}
PS: I am using a gcc compiler on windows.
In C when a function is not declared is is assumed to return int and compilation continues (btw this can lead to nasty bugs). If the function is declared without a type (as f1() is in your code it is assumed to return int. Not returning a value from a non-void function (as in your code) is undefined behavior.
So none of the points you mention are required to cause a compilation error. Undefined behavior is not required to prevent your program from running - the program might run and might even produce good looking results.
Firstly, you were not compiling with warnings enabled. You should usually invoke gcc with at least the -Wall switch - for your example code, that gives:
x.c: In function 'main':
x.c:7: warning: implicit declaration of function 'f1'
x.c: At top level:
x.c:15: warning: return type defaults to 'int'
x.c: In function 'f1':
x.c:16: warning: format '%d' expects type 'int', but argument 2 has type 'int *'
x.c:17: warning: control reaches end of non-void function
Secondly, the reason that it compiles is that all of the errors in it are of a form called "undefined behaviour", which means that the compiler isn't required to diagnose them and stop compilation - it can simply produce garbage results.
You would probably be happier if you used gcc's -Wall option to enable all warnings. Then you would see this while compiling:
C:\test>make
gcc -Wall prog.c -o prog
prog.c: In function 'main':
prog.c:7:5: warning: implicit declaration of function 'f1'
prog.c: At top level:
prog.c:14:1: warning: return type defaults to 'int'
prog.c: In function 'f1':
prog.c:16:8: warning: format '%d' expects type 'int', but argument 2 has type 'int *'
prog.c:17:1: warning: control reaches end of non-void function
You can also use -Werror to turn all warnings in to errors (so you are forced to fix them).
Try compiling again with the -Wall flag. That turns on all warnings, then you'll see plenty:
c.c: In function ‘main’:
c.c:7: warning: implicit declaration of function ‘f1’
c.c: At top level:
c.c:15: warning: return type defaults to ‘int’
c.c: In function ‘f1’:
c.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
c.c:17: warning: control reaches end of non-void function
Since you don't have any compile time errors, just warnings, the code compiles fine. It just won't execute very well.
f1 is considered to have the implicit prototype 'int f1(int)'.
Hence the call is valid.
f1 does not return anything whereas it is implicitly suppoed to return an int: the behaviour is undefined.
The compiler could warn you that the implicit prototype does not agree with the definition. It could also ask for a proper return. This is part of the checks that the static analysis inside the compiler could perform: gcc does not do it, others may.
In any case compilers usually do not guarantee anything about non ISO conforming programs.
If you add -Wall to your gcc commands you should get some warnings. I guess it works as old style C had loose function prototyping. And will assume unknown functions return int. The linker worked because there were no undefined symbols (f1 exists) but you got garbage as what was passed on the stack (i.e. nothing) was not what was expected.