如何修复分段错误?

时间:2015-02-01 18:38:35

标签: c

我写了这个简单的程序,当我运行它时,它说分段错误。我如何解决它?只运行第一行程序。

#include <stdio.h>


int main()
{
int items;
float price, pprice, amt, HST;

printf("Enter the number of items :", items);
    scanf(" %d", items);

printf("Enter the unit price :", price);
    scanf(" %f", price);

pprice = items * price;
printf("Purchase price :pprice\n");

printf("HST (13%) : 1.64\n");
HST = 1.64;
amt = HST + pprice;

printf("Total price: amt\n\n");



return 0;
}

2 个答案:

答案 0 :(得分:3)

scanf(" %d", items);

应该是

scanf(" %d", &items);
scanf(" %f", &price);

提供变量的地址。

编辑:

您的printf也有问题

printf("Enter the number of items :", items);

应该只是

printf("Enter the number of items :");

printf("Enter the unit price : %f", price);

请查看以下链接:

http://linux.die.net/man/3/printf

答案 1 :(得分:0)

分段错误通常发生在你不在scanf中使用&符号(&amp;)时,所以尝试将&符附加到变量

喜欢scanf(“%d”,&amp; items);