Variables with absurd values in the execution of the program in C

Asked

Viewed 52 times

-1

I’m doing some C exercises and the execution of my super ultra 5-line program is in trouble. I’ve seen several questions around suggesting that it might be an unrelated error that would be corrupting the variable, but I can’t figure out what it might be. This is happening in the other exercises as well. Follow the code and exit:

#include <stdio.h>
#include <math.h>
#include <locale.h>

int main(){
    setlocale(LC_ALL, "Portuguese");

    int numero;

    printf("Digite um número inteiro: ");
    scanf("%d", &numero);

    printf("\n O número %d elevado ao quadrado é: %.2f", &numero, pow(numero, 2));
    printf("\n A raiz quadrada de %d é: %.2f", &numero, sqrt(numero));
}

Digite um número inteiro: 4

 O número 6487580 elevado ao quadrado é: 16,00
 A raiz quadrada de 6487580 é: 2,00
--------------------------------
Process exited after 2.115 seconds with return value 0
Pressione qualquer tecla para continuar. . .

1 answer

-1


Here:

 printf("\n O número %d elevado ao quadrado é: %.2f", &numero, pow(numero, 2));

you are printing the address of the variable number and not the content of the variable number.

Spin:

#include <stdio.h>
#include <math.h>
#include <locale.h>

int main(){
    setlocale(LC_ALL, "Portuguese");

    int numero;

    printf("Digite um número inteiro: ");
    scanf("%d", &numero);

    printf("\n O número %d elevado ao quadrado é: %.2f", numero, pow(numero, 2));
    printf("\n A raiz quadrada de %d é: %.2f", numero, sqrt(numero));
}
  • thank you so much :D I was going to stay until Monday to break my head waiting for the course mediation to answer me just because of this damned &.

Browser other questions tagged

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