Reputation: 31
I was writing some basic code to learn the printf and scanf functions, however when integer are inputted via the scanf function, the printf function returns with completely different numbers. A similar thing happens when a character is inputted, only the printf function displays a square instead. Is there a problem with the code, or is it something else?
#include <stdio.h>
int main()
{
char letter;
int num1,num2;
printf("Enter any one keyboard character: ");
scanf("%c", &letter);
printf("Enter any two integers separated by a space: ");
scanf("%d %d", &num1, &num2);
printf("Numbers output %d and %d \n" , &num1, &num2) ;
printf("letter input %c\n" , &letter) ;
printf("stored at %p" , &letter) ;
return 0;
}
Whenever any integers or character is inputted,2686680 and 2686676 are displayed in place of the integers and a filled in rectangle is displayed instead of the character.
Any help would be much appreciate, thanks!
Upvotes: 3
Views: 2429
Reputation: 12658
&letter
mean the address of variable letter
is stored which the printf are correct. You should use just letter
in printf arguments as,
printf("%d\n", letter); // prints the value stored in letter
printf("%p\n", &letter); // prints the address where letter is stored in memory
Upvotes: 4