I was trying to create a program that inputs two number and outputs their sum. For this I must have to use two variables. I was just curious whether this can be done by using only one variable.
Note : user has to input two numbers.
#include<stdio.h>
int main()
{
int a, b;
scanf("%d%d",&a,&b);
printf("%d",(a+b));
return 0;
}
#include <stdio.h>
int main ( void )
{
int a[3];
scanf("%d", &a[0]); /* first number */
sscanf("%d", &a[1] ); /* second number */
a[2] = a[0] + a[1];
printf("sum is %d\n", a[0] + a[1] );
printf("sum stored in a[%d] is %d\n", 2, a[2] );
return 0;
}
Technically one variable, a pointer:
#include<stdio.h>
int main() {
int *nums = malloc(2 * sizeof(int));
scanf("%d%d",nums, (nums + sizeof(int)));
printf("%d",(*nums + *(nums + sizeof(int))));
return 0;
}
But no there isn't really an elegant way to use one variable for two inputs.
Note that I've considered the question like a challenge or a puzzle. Do not consider this answer good C practice. Obviously the cleanest way to make a sum of 2 values from input is to use 2 variables. I still find the challenge interesting though.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
printf("%g", fmin((scanf("%d", &a), a), 1.0/0.0 + rand()) + fmin((scanf("%d", &a), a), 1.0/0.0 + rand()));
return 0;
}
Works with negative values.
I'm using the comma operator which executes both expressions but only return the second one. So (scanf("%d", &a), a)
is like calling scanf("%d", &a)
and returns a
. I pass this result through a function (any function) as I want to prevent updating the value (to sum it with the new a). I have no idea if your compiler will call the left or right part of the big expression first but it doesn't matter as both are doing the same thing. Whichever executes first will be the first value from input.
fmin(x, 1.0/0.0 + rand())
makes sure nothing is inlined by the compiler. 1.0/0.0 is Infinity and would never be returned in fmin()
in our case. Compiler would inline this to x
normally but adding + rand()
to Infinity (which is still Infinity) seems to prevent it.
You can even do it by declaring "0" variable by using argc:
#include <stdio.h>
#include <math.h>
int main(int a)
{
printf("%g", fmin((scanf("%d", &a), a), 1.0/0.0 + rand()) + fmin((scanf("%d", &a), a), 1.0/0.0 + rand()));
return 0;
}
I've used this to test: https://www.onlinegdb.com/online_c_compiler
Adding Two numbers with using only one variable in C
Create a helper function with the 1 variable.
#include <stdio.h>
int scan_int(void) {
int a;
if (scanf("%d", &a) == 1) {
return a;
}
return 0;
}
int main(void) {
printf("Sum %d\n", scan_int() + scan_int());
return 0;
}
Note that scan_int() + scan_int()
, code could call either the left or the right scan_int()
first (or in parallel). Fortunately +
is commutative, so it makes no difference here.
The "trick" here is that there exist in sequence or in parallel, a 1st_call_scan_int::a
and 2nd_call_scan_int::a
. Still only one variable in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With