In C: passing and printing variables arguments of a function - c

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;
}

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

Some code of pointer

The code need to send to the function an int must to send just int and show the changes after the function. so I tried this:
int main(void)
{
int number = 0;
printf("Please enter some number: \n");
scanf("%d", &number);
printf("the number that you entered: %d\n", number);
inc(number);
printf("After the 'inc' function, your number is: %d\n", number);
system("PAUSE");
return 0;
}
/*
*/
void inc(int x)
{
int* px = &x;
*px= *px+1;
}
it's prints just the same number and doesn't change at all. help?
you must pass the parameter as a pointer
void inc(int *px)
{
*px= *px+1;
}
in your code you are just modifying the functions local copy of x
A hacktacularly terrible hackaround:
inc((int)&number);
// ...
void inc(int n)
{
++*(int*)n;
}
Don't do this. It won't work on all but certain archaic architectures.

c function calling error in printf()

#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));

Point or "reset" variables in C

If I have some thing like:
printf("\nEnter 2 numbers: \n");
scanf(" %d %d", &a, &b);
add (a,b)
int a,b;
{
printf ("%d", a+b);
}
Then want to run the block again, but with new variables of "nothing" like when the first printf statement is entered. Any suggestions?
First of all avoid using K&R C syntax
/* Your function
add (a,b)
int a,b;
{
printf ("Sum = %d\n", a+b);
}
*/
/* Use following style*/
void add (int a,int b)
{
printf ("Sum = %d\n", a+b);
}
int main()
{
int i,a,b; // Declare variables
int n=5; // Call it say n=5 times
for(i=0;i<n;i++) //Use a for loop to iterate for n times
{
printf("\nEnter 2 numbers: \n");
if(scanf(" %d %d", &a, &b)==2) // with 2 new inputs
add(a,b); //Call your add function
}
}

Resources