c function calling error in printf() - c

#include<stdio.h>
#include<conio.h>
int adder(int,int);
void main()
{
int a,b;
printf("enter nos");
scanf("%d%d",&a,&b);
adder( a,b);
printf("sum is %d",adder);
getch();
}
int adder(int x,int y)
{
return x+y;
}
this program is not working.I think the code is right.Can you point out the error?

adder is a function, what you should printf is its return value.
And as #JonathanLeffler said, it's better to add a newline at the end if you want to ensure the output appears timely. So,
change
adder( a,b);
printf("sum is %d",adder);
to:
int result = adder(a,b);
printf("sum is %d\n", result);
or to:
printf("sum is %d\n", adder(a, b));

Related

Why this program gives address in output?

#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
// scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(2, 7));
Please focus on this line why it gives address + 0 = 9//
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
// produces outPut : 199164000 + 0 = 9
You commented the scanf function and also didn't given values for the a and b. So it printed garbage values. You also need to pass a and b into the add(a,b) function.
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(a, b));
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
It is not printing any address. It is printing garbage values. You have not given any values to the variable a and b. So it will print garbage values. Why you commented scanf statement. Just stop commenting it and it will work.

Pointer variable not being updated for C

#include <stdio.h>
void gcdFinder(int num1, int num2, int *result);
int main()
{
int n1, n2, result;
printf("Enter 2 numbers: \n");
scanf("%d %d", &n1, &n2);
gcdFinder(n1, n2, &result);
printf("rGcd2(): %d\n", result);
return 0;
}
void gcdFinder(int num1, int num2, int *result)
{
printf("Initial : %d %d \n",num1,num2);
*result=num1;
if(num2 ==0 ){
return;
}else{
gcdFinder(num2,(num1%num2),&result);
}
}
I am trying to find the GCD the 2 inputs and storing the result into the result variable. However my code does not seem to work as it only stores one time.
As the output suggest my final value for num1 should be 1 however it is not stored and it remains as 4.
In the function gcdFinder change
gcdFinder(num2,(num1%num2),&result);
to
gcdFinder(num2,(num1%num2), result);
^
No & as result is already a pointer here
Notice:
In main it's correct to use &result because result in main is an int

How to get the largest integer using modular programming?

So, I've been trying to get two integers from the user and the program should return the value of the larger one. Here is my sample program:
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n%d is larger than the other", larger(num1, num2) );
}
int larger(int a, int b)
{
if (a>b)
{
printf("%d", a);
}
else if(b>a){
printf("%d", b);
}
}
But the problem here is that whenever I compile then run the program an integer value of 1 is placed beside the sentence "is larger than the other" ,while the largest integer (the integer that should be beside the 'is larger than the other') is placed above the integer 1.
SAMPLE OUTPUT:
Enter first number: 5
Enter second number: 3
5
1 is larger than the other
What should I do to place the larger value placed where it should be?
int larger(int a, int b)
Your function doesn't have a return statement . If you want largest value between two you need to return it from function . Like this -
int larger(int a, int b)
{
if (a>b)
{
//printf("%d", a); unnecessary as you print value in main
return a;
}
else if(b>a){
//printf("%d", b);
return b;
}
else
return a; //in this case take care of output message as both variables will be equal
}
larger needs to actually return the larger value: write return a;, and return b; in the appropriate places. And the output will look odd if you retain the printf calls in that function.
Currently your program behaviour is undefined as the return value is missing: the output is currently arbitrary.
You're also not dealing with all possibilities. What should happen if a and b are equal? You must return something on all control paths.
Your function isn't returning anything, you need to enable compiler warnings or alternatively, get a new compiler.
That being said, in case you aren't afraid of boolean logic, the function can simply be written as
static inline bool larger (int a, int b)
{
return a > b;
}
Naturally, you'll have to rewrite main() to check the result:
if(larger (x, y))
{
printf("x larger than y");
}
In int function larger(int a, int b) you should just compare and return the value rather than printing it.
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2, largeOftheTwo= 0;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
if(num1 != num2)
printf("\n%d is larger than the other", larger(num1, num2));
else
printf("\n%d is equal to %d", num1, num2);
return 0;
}
int larger(int a, int b)
{
if (a>b)
return a;
else
return b;
}

Function not passing to main [duplicate]

This question already has answers here:
permanently changing value of parameter
(6 answers)
Closed 8 years ago.
I don't get any errors but I do not get the correct value. It keeps on printing 0! It looks like it's not reading my function and I really don't know what it could be.
#include <stdio.h>
#include <math.h>
int rec(int base, int ex,int ans);
int main()
{
int base;
int ex;
int ans;
for(ex=2;ex!=1;){
printf("Enter a base and an exponent\n");
scanf("%d %d",&base,&ex);
rec(base,ex,ans);
printf("%d raised to the %d is %d \n", base, ex, ans);
}
return 0;
}
int rec(int base, int ex,int ans)
{
ans=pow(base, ex);
return ans;
}
You have two different ans in your code and you are interpreting them incorrectly. Assign the return value of rec to ans and delete the one in rec, since it is pointless. Here we go:
int main() {
int base;
int ex;
int ans;
for(ex=2; ex!=1;) {
printf("Enter a base and an exponent\n");
scanf("%d %d",&base,&ex);
ans = rec(base,ex);
printf("%d raised to the %d is %d \n", base, ex, ans);
}
return 0;
}
int rec(int base, int ex) {
return pow(base, ex);
}

In C: passing and printing variables arguments of a function

I am trying to write a program which uses function with variable number of arguments. An additional task is to print all the arguments of every function call separately. The code is as follows:-
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
int mul(int x,...);
int main()
{
int a=1,b=2,c=3,d=4,x;
printf("The product of %d is :%d\n",a,mul(1,a));
printf("The product of %d, %d is :%d\n",a,b,mul(2,a,b));
printf("The product of %d, %d, %d is :%d\n",a,b,c,mul(3,a,b,c));
printf("The product of %d, %d, %d, %d is :%d\n",a,b,c,d,mul(4,a,b,c,d));
return 0;
}
int mul(int x,...)
{
int i,prod=1;
va_list arglist;
va_start(arglist, x);
for(i=0;i<x;i++)
{
prod*=va_arg(arglist,int);
}
printf("\n");
for(i=0;i<x;i++)
{
printf("The argument is %d\n",va_arg(arglist,int));
}
va_end(arglist);
return prod;
}
The output of this program is as follows:-
The other piece of code is:-
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
int mul(int x,...);
int main()
{
int a=1,b=2,c=3,d=4,x;
printf("The product of %d is :%d\n",a,mul(1,a));
printf("The product of %d, %d is :%d\n",a,b,mul(2,a,b));
printf("The product of %d, %d, %d is :%d\n",a,b,c,mul(3,a,b,c));
printf("The product of %d, %d, %d, %d is :%d\n",a,b,c,d,mul(4,a,b,c,d));
return 0;
}
int mul(int x,...)
{
int i,prod=1;
va_list arglist;
va_start(arglist, x);
for(i=0;i<x;i++)
{
prod*=va_arg(arglist,int);
}
printf("\n");
va_end(arglist);
va_start(arglist,x);
for(i=0;i<x;i++)
{
printf("The argument is %d\n",va_arg(arglist,int));
}
va_end(arglist);
return prod;
}
The output is as follows:-
Why is this difference? Any help?
In the first example you are missing two lines:
va_end(arglist);
va_start(arglist,x);
This means that after doing the multiplication you are reading past the end of the parameters. The values that are displayed are whatever happened to be on the stack.
va_arg(va_list ap, type) retrieves next argument in the argument list.So in the first code you are consuming the arguments after one loop . Instead of 2nd code you can use the following code which prints argument and maintains multiplication in a single loop
int mul(int x,...)
{
int i,m,prod=1;
enter code here
va_list arglist;
enter code here
va_start(arglist, x);
for(i=0;i<x;i++)
{
m=va_arg(arglist,int);
prod*=m
printf("The argument is %d\n",m);
}
printf("\n");
return prod;
}

Resources