You really need to spend several days reading a book on Programming in C.
You also should look into websites like C reference.
In C, every function should have been declared (perhaps in some included header) before being used.
So move your max_min
function before your main
, or declare it:
int max_min(int a,int b);
at declaration time, you don't need to name formals, so the following shorter declaration is possible:
int max_min(int,int);
BTW, max_min
is really a poor and confusing name (since you don't compute the minimum, only the maximum). You mean max
or perhaps my_max
or maximum
.
Don't forget to compile with all warnings and debug info (e.g. gcc -Wall -g
if using GCC compiler). Then use the debugger (e.g. GNU gdb
). Perhaps a clever compiler would notice that you don't cover the case when a
is equal to b
.
You'll need to test your program for several inputs (and you could also use formal methods to prove its correctness w.r.t. specifications, e.g. with the help of Frama-C). Instead of recompiling your code with various values for a
and b
, you might get them from the program arguments passed to main
(converting them with atoi), e.g.
int main(int argc, char**argv) {
int a= 10;
int b= 5;
if (argc>1)
a = atoi(argv[1]);
if (argc>2)
b = atoi(argv[2]);
printf("a=%d b=%d\n", a, b);
etc....
Then you could run your program (in some terminal) with ./myprog 3 4
to test with a
equal to 3 and b
equal to 4. Actually you'll run that in the debugger (e.g. gdb --args ./myprog 3 4
).
BTW, you could also read (with scanf) the values of a
and b
.