Problem with overflow in c, my variable bursts

Asked

Viewed 64 times

0

This code is used to add two binary numbers and write the result but in the output this appears:

warning: overflow in conversion from 'long long int' to 'long int' changes value from '10100101010' to '1510166418' [-Woverflow]

I tried to change the long to long int but I don’t know how I can fix it. pfv someone help me.

this is the code

#include <stdio.h>
 
int main()
{
 
     long binario1=10100101010, binario2=10100101010;
    int i = 0, sobra = 0, soma[10];
 
    printf("primeiro numero binario: %ld\n", binario1);
    
    printf("segundo numero binario: %ld\n",binario2);
    
    while (binario1 != 0 || binario2 != 0)
    {
        soma[i++] =(binario1 % 10 + binario2 % 10 + sobra) % 2;
        sobra =(binario1 % 10 + binario2 % 10 + sobra) / 2;
        binario1 = binario1 / 10;
        binario2 = binario2 / 10;
    }
    if (sobra != 0)
        soma[i++] = sobra;
    --i;
    printf("Soma dos dois numero binarios: ");
    while (i >= 0)
        printf("%d", soma[i--]);
    return 0;
} 
  • Forget it, this is not how you deal with this problem. Make the code again now using string, because you don’t convert numbers, number is number. What you can do is create a text with binary notation and present it if you want. What the C library already does for you automatically is to turn a number into decimal text, and setting it right can do in hexadecimal but it is always text. You can view more in https://answall.com/q/324260/101 and https://answall.com/q/216128/101 and https:///pt.stackoverflow.com/q/152947/101

  • You don’t have binaries there, you have decimals. This: 10,100,101,010 is by extension ten billion, one hundred million, one thousand and ten, it’s not what you think it is.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.