I am learning C programming and wrote a simple program to learn function in C. I have used two functios here, although the first one works but not the second one !
Here is the simple code:
#include<stdio.h>
void main() {
int a,b,c,sum;
printf("Input your numbers one by one:\n");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
printf("You have put %d, %d, %d\n\n", a, b, c);
max (a,b,c);
min (a,b,c);
}
void max (int a, int b, int c) {
int sum;
a=sum;
if(sum>b)
sum=b;
if(sum>c)
sum=c;
}
void min (int f, int g, int h) {
int sum;
sum=f;
if(sum<g)
sum=g;
if(sum<h)
sum=h;
printf("The lowest value is:%d\n\n\n",sum);
}
Can anyone tell me why this happens and the solution?
None of your function should work. The first one set in the variable
athe current value ofsumwhich is unitialized.Then when searching for the MAX value, you check if current sum (supposed to be
a) is greater thanband if it is you storebvalue insum.Then if you want to call a function before it is defined you need to put the prototype of the function at the beginning of your file. As you are calling
min()andmax()before their definition you are having compilation errors.It should be like this: