我正在通过示例使用C学习C.有一个问题,我们需要取一个输入整数并将其平方并将其打印到屏幕上。我有一个奇怪的错误,无论我输入什么Int,它都会给我这个输出:
Please enter a number.
5
Number = 2686764 Square of Number = 2686760
这是我写的程序。
#include <stdio.h>
#include <conio.h> //for getch()
main(){
int number, square;
printf("Please enter a number. \n");
scanf("%d", &number);
square = number*number;
printf("\n Number = %d ", &number);
printf("\t Square of Number = %d", &square);
getch();
}
答案 0 :(得分:5)
您正在打印number
和square
变量的内存地址,而不是其值。
请改为尝试:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
答案 1 :(得分:4)
您的printf
语句不正确。在这种情况下不要使用&
:
printf("\n Number = %d ", number);
printf("\t Square of Number = %d", square);
答案 2 :(得分:1)
要添加上述答案......请看这个例子:
int number = 5;
printf(" %d\n", number); // Prints value of 'number'.
printf(" %p\n", &number); // Prints address of 'number'.
printf(" %d\n", *(&number)); // Prints value at address of 'number' ...