Hidden value of variable

Asked

Viewed 53 times

0

When I put to show the numbers that were exchanged the program just shows nothing, I do not understand why.

Read two numbers by storing them in the variables num1 and num2. Check if the value of num1 is greater than the value of num2, and if so, exchange the contents of the variables

#include <stdio.h>

int main(){
/*Ler dois números, armazenando-os nas variáveis num1 e num2. Verificar se o valor de
num1 é maior que o valor de num2 e, em caso positivo, trocar os conteúdos das variáveis*/


int num1, num2, x;
printf("Digite o valor do primeiro numero\n");
scanf("%d", &num1);
printf("Digite o valor do segundo numero\n");
scanf("%d", &num2);
x=num1;
if (num1<num2){
    num1=num2;
    num2=x;
    printf("O valor do primeiro numero é:\n", num2);
    printf("O valor do segundo numero é:\n", x);
}

return 0;

}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

If I understood correctly you have to print the two original values in a different way, then it makes no sense not to print one of them and print a temporary value that is the same as the other. Not using a suitable variable name helps you make the mistake. Besides this is not having the number printed, it is only sending a text, to print the number has to say where it will be placed and it is used in this case the placeholder %d for an entire number. There are more misinterpretations, so it looks better:

#include <stdio.h>

int main() {
    int num1, num2;
    printf("Digite o valor do primeiro numero\n");
    scanf("%d", &num1);
    printf("Digite o valor do segundo numero\n");
    scanf("%d", &num2);
    if (num1 > num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    printf("O valor do primeiro numero é: %d\n", num1);
    printf("O valor do segundo numero é: %d\n", num2);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

0

If num1 is larger than num2 will print them changed, otherwise nothing will happen.

#include <stdio.h>

int main(){
/*Ler dois números, armazenando-os nas variáveis num1 e num2. Verificar se o valor de
num1 é maior que o valor de num2 e, em caso positivo, trocar os conteúdos das variáveis*/


int num1, num2, x;
printf("Digite o valor do primeiro numero\n");
scanf("%d", &num1);
printf("Digite o valor do segundo numero\n");
scanf("%d", &num2);
x=num1;
if (num1>num2){
    num1=num2;
    num2=x;
    printf("O valor do primeiro numero é: %d\n", num1);
    printf("O valor do segundo numero é: %d\n", num2);
}

return 0;

}

Browser other questions tagged

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