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;
}
Related
I need to write a function to compute a^b but I am not allowed to use pow. Any ideas? I am lost.
It looks like problem is in main now...
Somewhere it gets that vys is what i characterise it. So if i set that vys=1 in main i get 1 in output..
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
void multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *=b;
i++;
}
return vys;
}
main(void)
{
int b=0, n=0, vys=1;
printf("Give numbers b and n but they must be in interval <0,10>!\n");
scanf("%d %d", &b, &n);
if ((b < 0 || b>10) || (n<0 || n>10))
{
printf("Numbers are not in interval <0,10>!\n");
}
else
{
printf("Number is in interval so i continue...\n");
sleep(2);
vys= multiplied(&b, &n);
printf("%d", vys);
}
Let's be explicit.
First, this
void multiplied(int *b, int *n)
returns an int, so say so.
int multiplied(int *b, int *n)
Next, you initialised variables in main: do the same here.
int i, vys;
Like this:
int i=1, vys=1;
Now let's look at the loop:
while (i<=n)
{
vys=*b**b;
i++;
}
As it stands, you are setting vys to something over and over again in the loop.
You want to multiply up, e.g. 2, then 2*2, then 2*2*2, .... if you want a power of two:
while (i<=n)
{
vys *= *b;
i++;
}
Now, you don't need to pass pointers.
int multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *= b;
i++;
}
return vys;
}
Edit:
Watch out for when you call the function:
main(void)
{
int b=0, n=0, vys;
//input and checking code as you have it
multiplied(&b, &n); //<---- return ignored
printf("%d", vys); //<-- print uninitialsed local variable
}
Change you last two lines:
vys = multiplied(&b, &n); //<---- return captured
printf("%d", vys); //<-- print returned variable
Edit 2:
With the change to use int in the function and not pointers, pass the ints not their addresses:
vys = multiplied(b, n); //<---- pass the ints not their addresses
printf("%d", vys); //<-- print returned variable, which should vary now
Here you have a simple code:
#include <stdio.h>
long long intpow(int a, int b)
{
long long tempres = 1;
while(b--)
tempres *= a;
return tempres;
}
int main(void) {
printf("%lld\n", intpow(5,10));
return 0;
}
you need much larger int to accommodate the result.
You can play with it yourself: https://ideone.com/4JT6NQ
#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);
}
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.