#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
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.
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;
}
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);
}
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.
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.
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);
}
Related
let's assume the following code in c:
#include <stdio.h>
#include <cs50.h>
int test (int a, int b);
int main(void)
{
test(2,3);
}
int test (int a, int b)
{
int c = a+b;
printf("%d \n", test(a,b));
return c;
}
why is it not possible to print the value of test without having to save it in a variable before and print the variable? I get the error:
function.c:12:1: error: all paths through this function will call itself [-Werror,-Winfinite-recursion]
Thank you!
#include <stdio.h>
#include <cs50.h>
int test (int a, int b);
int main(void)
{
test(2,3);
}
int test (int a, int b)
{
int c = a+b;
printf("%d \n", test(a,b));
return c;
}
As the compiler message says, the function will call itself, because, in printf("%d \n", test(a,b));, the code test(a,b) calls test. Inside that call to test, the function will call itself again, and this will repeat forever (up to the limits of the C implementation).
To print the return value of the function, do it outside the function:
#include <stdio.h>
int test(int a, int b);
int main(void)
{
printf("%d\n", test(2, 3));
}
int test(int a, int b)
{
return a+b;
}
The error message is quite clear. The test-function calls itself. And inside that call, it calls itself again, (and again, and again...).
It will never complete.
This is a type of infinite loop, commonly called infinite recursion.
Perhaps what you want is?
#include <stdio.h>
#include <cs50.h>
int test (int a, int b);
int main(void)
{
test(2,3);
}
int test (int a, int b)
{
int c = a+b;
printf("%d \n", c); // Show the result of the calculation
// but without calling this function again.
return c;
}
The function with the pass by value works like it ought to, I believe, as it prints out the value 0 whenever I enter two numbers. However the adderRef (pass by reference function) doesn't work. All it prints out is "the pass by reference of c is" and that's it. There is no value or anything. I just wanted to inquire whether there was something wrong with my syntax or something....
Okay guys sorry about the question being vague and for my errors. It's my first time asking on stackoverflow and I should have been more mindful. I'm aware of my error and why I made it. I got muddled in class when my teacher was altering the code a bit and I copied it down incorrectly/ Thanks everyone for your help. Yes i was indeed quite dumb .
#include <stdio.h>
#include <stdlib.h>
void adderval(int a, int b);
void adderRef(int *, int *);
int main()
{
int a, b, c = 0;
printf("enter two numbers.\n");
scanf("%d %d", &a, &b);
adderval(a, b);
printf("the pass by value of c is %d \n", c);
adderRef(&a, &b);
printf("the pass by reference of c is \n", c );
return 0;
}
void adderRef(int *a, int *b )
{
int c;
c = *a + *b;
}
void adderval(int a, int b )
{
int c;
c = a + b;
}
corrected as below.
#include <stdio.h>
#include <stdlib.h>
void adderval(int a,int b,int c);
void adderRef(int a,int b,int *c );
int main()
{
int a,b,c=0;
printf("enter two numbers.\n");
scanf("%d %d",&a,&b);
adderval(a,b,c);
printf("the pass by value of c is %d \n",c);
adderRef(a,b,&c);
printf("the pass by reference of c is %d \n",c );
return 0;
}
void adderRef (int a, int b, int *c )
{
*c = a + b;
}
void adderval (int a , int b,int c )
{
c=a+b;
}
I'm trying to learn call functions and I've come across a problem where I'm being told I need I need to declare a, b, c, and d, but the point of the program is to prompt the user for these numbers and then sum them.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int f(int a, int b, int c, int d);
int g(int b, int c, int d);
int h(int c, int d);
int i(int d);
int
main(int argc,char **argv)
{
int result;
result = f(a,b,c,d);
printf("Value of a?");
scanf("%d",a);
printf("Value of b?");
scanf("%d",b);
printf("Value of c?");
scanf("%d",c);
printf("Value of d?");
scanf("%d",d);
printf("Your result is %d",result);
return 0;
}
int
f(int a, int b, int c, int d)
{
return a + g(b,c,d);
}
int
g(int b, int c, int d)
{
return b + h(c,d);
}
int
h(int c, int d)
{
return c + i(d);
}
int
i(int d)
{
return d + d;
}
The specific warning is
call.c:16:13: error: ‘a’ undeclared (first use in this function)
result = f(a,b,c,d);
and it repeats for b, c, and d.
Can anyone tell me what I've done wrong? I put the function signatures at the top where int a, int b, int c, and int d are already defined so I'm confused as to what I've done incorrectly.
Edit: Question has been solved! The code should look like
int result;
int a;
int b;
int c;
int d;
printf("Value of a?");
scanf("%d",&a);
printf("Value of b?");
scanf("%d",&b);
printf("Value of c?");
scanf("%d",&c);
printf("Value of d?");
scanf("%d",&d);
result = f(a,b,c,d);
printf("Your result is %d",result);
return 0;
You need to declare those variables in main, try changing your code to
int result, a, b, c, d;
Also scanf expects pointers in it's variadic portion of arguments,
so you're going to need to use the & reference operator to take the address of a
Example: scanf("%d", &a);
And do this for all of your scanf calls.
This question already has answers here:
C function declaration
(4 answers)
Closed 8 years ago.
This is a small program to multiply and add two numbers using functions..
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
int add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
return 0;
}
int mult(int a,int b)
{
return a*b;
}
Although, I don't think that I need to have a return type add function,so I tried to use this code...
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
void add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
}
int mult(int a,int b)
{
return a*b;
}
but it tells me that there's an error for mismatch type declaration??
You need to provide a prototype before the first use:
void add(int a, int b); /* This tells the compiler that add() takes */
/* two ints and returns nothing. */
int main() {
...
add(a, b);
}
void add(int a, int b) {
...
}
Otherwise the compiler is obliged to assume that add() returns an int.
For more information, see Must declare function prototype in C?
First, you have to put declaration of functions before using it:
void add(int a,int b);
int mult(int a,int b);
Then you can put its definition wherever you want.
Or you can declare & define them before using.
I am learning recursion and i encountered a conceptual doubt while solving the problem of calculation of remainder when a positive integer is a is divided by a positive integer b.
My code is:
#include<stdio.h>
#include<stdlib.h>
int x;
int rem(int a,int b)
{
x=a;
if(x>=b)
{
x=x-b;
rem(x,b);
}
printf("%d\n",x);
return x;
}
int main()
{
int a,b;
printf("Enter a & b\n");
scanf("%d %d",&a,&b);
int y =rem(a,b);
printf("rem is :%d",y);
return 0;
}
Its working fine. I have learned that for every call a new set of formal parameters and local variables are created.
So i experimented it by printing x on return of every recursive call!
But it is printing 1 1 1 1. Why is the value of x corresponding to a particular call not printed. ?
Why only the last modified value printed?.. Is that because i declared 'x' as global?
In this case perhaps you need only to move your print up
int rem(int a,int b)
{
x=a;
printf("%d\n",x);
if(x>=b)
{
x=x-b;
rem(x,b);
}
return x;
}
But I think you should avoid the use of global variables in a recursive alrotithm. It could make the algorithm very difficult to reason about. Recursive functions are better to be 'pure functions'.
It is because while x >= b, rem() is repeatedly called before printf()s are called. Only after x < b will the printf()s are called as each call on rem() unwinds.
You might want to make x local to rem() to get the desired result.
Ignoring issues with checking the return value from scanf() and that the two entered values are both positive, etc, I think you can and should avoid x altogether. You could use:
#include <stdio.h>
static int rem(int a, int b)
{
if (a >= b)
a = rem(a-b, b);
printf("%d\n", a);
return a;
}
int main(void)
{
int a, b;
printf("Enter a & b\n");
scanf("%d %d", &a, &b);
int y = rem(a, b);
printf("rem(%d, %d) is: %d\n", a, b, y);
return 0;
}
This code captures the return value from rem() at each level of recursion. In this case, because the returned value doesn't change as the recursion unwinds, you could use the global variable x, but there is no need for it, and you should avoid global variables whenever you can.
#include<stdio.h>
#include<conio.h>
int fun(int,int);
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
fun(a,b);
//printf("%d",fun(a,b));
}
int fun(int a,int b)
{
if(a<b)
printf("%d",a);
if(a>=b)
a=fun(a-b,b);
return a;
}