Uttam Manher
Uttam Manher

Reputation: 152

Adding Two numbers with using only one variable in 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;
}

Upvotes: 7

Views: 9828

Answers (10)

Ankush Rawat
Ankush Rawat

Reputation: 311

One way to do it is.

#include <stdio.h>
int x;

int enter(){
scanf("%d",&x);
return x;
}     

int main()
{
    x=enter()+enter();
    print("sum of two number is %d",x);
    return 0;
}

Another way to do it..

#include <stdio.h>
int main()
{ 
int x;
scanf("%d",&x);
printf("next no. ");
 x= x*(scanf("%d",&x))+x;
printf("%d",x);
return 0;
}

Although the second one is not consistent, in some compiler it works perfectly and in some, it doesn't

Upvotes: 1

Grant Sanders
Grant Sanders

Reputation: 337

Try this on for size. It uses fixed width C99 types to guarantee that memory alignment works as intended. It even does the 32-bit arithmetic in a uint64_t type to prevent overflow issues. This will work with any of the other fixed-width integer types, signed or unsigned, with trivial modification.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main (void)
{
    uint64_t a;

    scanf ("%" SCNu32 "%" SCNu32, (uint32_t*)&a, (uint32_t*)&a + 1);

    printf ("%" PRIu64 "\n", (uint64_t)((uint32_t*)&a)[0] +
        ((uint32_t*)&a)[1]);
}

The above code was modified from my original answer, seen below. I removed the void* cast in (uint32_t*)(void*)&a because it was unnecessary. I also cleaned up the scanf arguments to increase readability, and added a new line to the end of the printf format argument.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main (void)
{
    uint64_t a;

    scanf ("%" SCNu32 "%" SCNu32,
        &((uint32_t*)(void*)&a)[0], &((uint32_t*)(void*)&a)[1]);

    printf ("%" PRIu64, (uint64_t)((uint32_t*)(void*)&a)[0] +
        ((uint32_t*)(void*)&a)[1]);
}

Upvotes: 0

CaptainOfHacks
CaptainOfHacks

Reputation: 36

#include <stdio.h>
int main()
{
    long long int buffer=0;
    scanf("%d",(int*)&buffer);
    scanf("%d",(int *)&buffer+1);
    printf("\nsum is %d\n",*((int*)&buffer)+*((int*)&buffer+1));
    return 0;
}

Upvotes: 0

chux
chux

Reputation: 153338

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.

Upvotes: 5

Winter
Winter

Reputation: 4095

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

Upvotes: 5

0___________
0___________

Reputation: 67476

Only one variable - no tricks. As many numbers can be added as you want :)

#include <stdio.h>

int ScanAndAdd(void)
{
    int a;
    if(scanf("%d", &a) != 1) return 0;
    return a + ScanAndAdd();
}

int main()
{
    printf("%d\n", ScanAndAdd());

    return 0;  /**/
}

Upvotes: 1

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

OK, there's been quite a few interesting answers, but weirdly nobody has thought of the obvious way to store 2 ints in a single variable - structs:

#include<stdio.h>

typedef _in struct {
    a int
    b int
} inp;

int main(void)
{
    inp input;
    scanf("%d%d",&input.a,&input.b);
    printf("%d",input.a+input.b);
    return 0;
}

Upvotes: 2

maryf
maryf

Reputation: 150

int main(void) {
    int *num = malloc(sizeof(int)*2);
    scanf("%d %d", num, num+1);
    printf("%d\n", num[0] + (num[1]));
}
  • int *num = malloc(sizeof(int)*2); //two int space
  • scanf("%d %d", num, num+1); // num (pos 0), num+1 (pos1)
  • printf("%d\n", num[0] + (num[1])); //the sum of the positions

Upvotes: 1

ron
ron

Reputation: 995

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

Upvotes: 11

agillgilla
agillgilla

Reputation: 889

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.

Upvotes: 5

Related Questions