Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Two numbers with using only one variable in C

Tags:

c

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;
}
like image 990
Uttam Manher Avatar asked Aug 01 '18 17:08

Uttam Manher


4 Answers

#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;
}
like image 99
ron Avatar answered Dec 16 '22 00:12

ron


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.

like image 35
agillgilla Avatar answered Dec 15 '22 23:12

agillgilla


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

like image 41
Winter Avatar answered Dec 15 '22 22:12

Winter


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.

like image 42
chux - Reinstate Monica Avatar answered Dec 15 '22 23:12

chux - Reinstate Monica