printf does not show expected value

1

I'm having difficulty displaying some printf , for example:

printf("\nDigite as horas : %d");

And at the time of displaying the message, it appears with a side number, for example:

  

Enter the hours: 89676

What's wrong with that? I declare the correct variables, but the display shows this number, what can I do to get them out?

    
asked by anonymous 11.04.2015 / 02:20

2 answers

4

Is this all that's the same line? So it makes sense to get a "crap" on the screen. The %d indicates that an integer value will be placed there. What value are you passing to the function to put there? None. Then he takes whatever he finds in his memory. If you pass a value, it will be printed.

On the other hand, I think you're wanting to use scanf() to read the times:

#include <stdio.h>

int main(void) {
    int i;
    printf("\nDigite as horas : %d"); //pega qualquer coisa para exibir
    printf("\nDigite as horas : %d", 12); //aqui imprime o 12

    printf("\nDigite as horas :"); //não tem dados variáveis para imprimir
    scanf ("%d", &i); //pede o dado aqui
    return 0;
}

See working on ideone .

    
11.04.2015 / 02:30
0

If you want to enter the hours first you should receive the times for example

int main(){
    int horas;
    printf("Digite as horas: ");
    scanf("%d", &horas);
    // Depois devolve o valor lido através de um printf
    printf("São %d horas", horas);
    return 0;
}
    
20.04.2015 / 22:06