C程序输出错误

时间:2017-05-03 15:30:22

标签: c

#include<stdio.h>

int main() {
  int m,n;
  float cake;

  printf("Enter total kgs of cake:");
  scanf("&d", &n);

  printf("Enter the number of friends:");
  scanf("&d", &m);

  cake  = m/n;
  printf(He has to distribute %f kg cake to each of his %d friends", 
         &cake, &m);

}

当我在codeblocks中运行这个程序时,它要求我输入总kgs of cake,当我输入它并点击enter时,程序只打印下面printf函数中的所有其他内容,而不是让我输入朋友的数量。

3 个答案:

答案 0 :(得分:4)

FROM python:3.4-alpine
RUN pip3 install -U setuptools
RUN pip3 install -U pylint

您需要将其更改为

scanf("&d", &n);

scanf("&d", &m);

此外,在printf函数中,您不应传递变量的地址

答案 1 :(得分:3)

&#34;&amp; d&#34;保存特定变量的地址。所以,你需要改变     scanf("&d",&m)scanf("%d",&m)scanf("&d",&n)scanf("%d",&n)。这里%d是整数变量的占位符。 同样,在最终的print语句中,&符号&打印的地址不是值,因此在最终的printf语句中删除&

答案 2 :(得分:1)

您的代码中存在几个问题。首先,scanf()应该是这样的:

scanf("%d", &var);

下一个关闭在您的printf声明中。在var部分中,您执行了&cake&m这是错误的,因为您要打印变量的地址。要解决此问题,请删除&

相关问题