Search code examples
cinputsumoutput

values ​different from the sum of what was to be c language


#include <stdio.h>

int main() {
    int A, B;
    int SOMA = A+B;
    scanf("%d%d", &A, &B);
    printf("SOMA = %d\n", SOMA);

    return 0;
}

/*
INPUT --> OUTPUT
30 10 --> SOMA = 16
1  3  --> SOMA = 16
300 1000 --> SOMA = 16
*/

why am i getting these results instead of the sum? I wanted the message "SOMA = sumValue" with the end of the line.


Solution

  • int SOMA = A+B;
    scanf("%d%d", &A, &B);
    

    You are adding A and B before the user has input any values. Reverse those two.