If you don't have prototypes for get and set_alpha then the compiler doesn't know what their parameters and return values are. Each source file is compiled independently. If the prototypes aren't listed in main.c
then the compiler has to guess at the function signatures. It wrongly guesses that everything's an int
, as in int set_alpha(int a)
and int get_alpha()
. Oops!
The fix:
void set_alpha(double a);
double get_alpha();
int main() {
...
}
The best thing to do is to create a separate header file block.h
and put the prototypes there. Also to make sure to enable all your compiler's warnings (e.g. gcc -Wall
) so you don't get bit by this again.
block.h
#ifndef BLOCK_H
#define BLOCK_H
void set_alpha(double a);
double get_alpha();
#endif
main.c
#include "block.h"
int main() {
...
}
block.c
#include "block.h"
...