Home > Back-end >  values ​different from the sum of what was to be c language
values ​different from the sum of what was to be c language

Time:05-30

#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.

CodePudding user response:

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.

CodePudding user response:

C is not Excel. This:

int SOMA = A B;

Does not tie the value of SOMA to the value of A B. This sets SOMA to the current value of A B, neither of which has been initialized.

You need to read the values of A and B first, then set the value of SOMA based on that.

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

CodePudding user response:

C is an imperative language. SOMA = A B is executed where you wrote it, i.e. before you even saved the values the user entered in A and B.

(This is really basic. Maybe find a better introduction to C!)

  • Related