-1
#include<stdio.h>
int main(void) 
{
  int a=3,b=4;
  int c;
  c = max_min(a,b);
  printf("the max no is %d\n",c);
}
int max_min(int a,int b)
{
  if(a>b)
    return a;
  else if(b>a)
    return b;
}

Error: 'max_min' was not declared in this scope c = max_min(a,b);

Please help me out

m7913d
  • 10,244
  • 7
  • 28
  • 56
Ramkumar
  • 1
  • 3

6 Answers6

2

Either define you function before main or give a forward declaration of your function. Like this -

#include<stdio.h>
int max_min(int a,int b);       // forward declaration 
int main(void){
 // your code

}
// rest of code

You need to define or provide a forward declaration before using a function else compiler will throw an error as it does not see function definition or declaration till that point.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

Declare it. I.e. put int max_min(int a,int b); above main. This will tell the compiler about the function

i.e.

#include<stdio.h>

int max_min(int a,int b);

int main(void) 
{
  int a=3,b=4;
  int c;
  c = max_min(a,b);
  printf("the max no is %d\n",c);
}
int max_min(int a,int b)
{
  if(a>b)
    return a;
  else if(b>a)
    return b;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

Add a prototype to tell the compiler that you have defined the function min_max later.

#include<stdio.h>

int max_min(int a,int b);

int main(void) 
{
 int a=3,b=4;
 int c;
 c = max_min(a,b);
 printf("the max no is %d\n",c);
 }
int max_min(int a,int b)
{
if(a>b)
return a;
else if(b>a)
 return b;
}

or define min_max before main

#include<stdio.h>

int max_min(int a,int b)
{
if(a>b)
return a;
else if(b>a)
 return b;
}

int main(void) 
{
 int a=3,b=4;
 int c;
 c = max_min(a,b);
 printf("the max no is %d\n",c);
 }
XZ6H
  • 1,779
  • 19
  • 25
1

You need a function prototype that declares the function, so the compiler knows what is being called from main.

#include<stdio.h>

int max_min(int a,int b);       // function protoype

int main(void) 
{
    int a=3,b=4;
    int c;
    c = max_min(a,b);
    printf("the max no is %d\n",c);
}

int max_min(int a,int b)
{
    if(a>b)
        return a;
    else if(b>a)
        return b;
}

BTW the compiler should report "not all control paths return a value" since there is no value returned by the function when a == b.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Try either one of it

First Declare the method before Main method

#include<stdio.h>
int max_min(int,int);
int main(void) 
{
  int a=3,b=4;
  int c;
  c = max_min(a,b);
  printf("the max no is %d\n",c);
}
int max_min(int a,int b)
{
  if(a>b)
   return a;
  else if(b>a)
   return b;
}

Or Specify the function before the main method

 #include<stdio.h>
 int max_min(int a,int b)
{
  if(a>b)
   return a;
  else if(b>a)
   return b;
}
int main(void) 
{
  int a=3,b=4;
  int c;
  c = max_min(a,b);
  printf("the max no is %d\n",c);
}
Abi
  • 724
  • 6
  • 22