简体   繁体   English

我的 c 程序出现未知错误

[英]i am getting a unknown error in my c program

Code:代码:

#include <stdio.h>

void main()
{
   int s1,s2,s3,s4,s5,sum;
   float per;
   printf("Enter subject 1 marks out of 100 \n");
   scanf("%d",s1);
   printf("Enter subject 2 marks out of 100\n");
    scanf("%d",s2);
   printf("Enter subject 3 marks out of 100 \n");
    scanf("%d",s3);
   printf("Enter subject 4 marks out of 100\n");
    scanf("%d",s4);
   printf("Enter subject 5 marks out of 100\n");
    scanf("%d",s5);

   sum=s1+s2+s3+s4+s5;
    per=sum/100;
    if (per>60 && per<70){
      printf("your percentage is %d and you get 10% schoolarship",per)
    ;}
   else if (per>70.1 && per<90){
      printf("your percentage is %d and you get 20% schoolarship",per)
    ;}
     else {
      printf("your percentage is %d and you get 30% schoolarship",per)
    ;}
}

Output: Output:

这是代码的输出

I am trying to make a percentage calculator and it shows a weird output.我正在尝试制作一个百分比计算器,它显示了一个奇怪的 output。

What am I doing wrong?我究竟做错了什么?

When you call scanf, it is important to pass in the address of the variable you want to store.当你调用 scanf 时,重要的是传入你要存储的变量的地址。 Otherwise, scanf will not behave as you expect.否则,scanf 将不会像您预期的那样运行。

Right now, you are not passing in the address to scanf;现在,您没有将地址传递给 scanf; but rather the variable itself.而是变量本身。 So you should do something like:因此,您应该执行以下操作:

    scanf("%d",&s1);

instead.反而。

https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm

I recommend reading a little bit about how scanf works at the following link.我建议阅读以下链接,了解 scanf 的工作原理。

"Following is the declaration for scanf() function. “以下是 scanf() function 的声明。

int scanf(const char *format, ...)" int scanf(const char *format, ...)"

Additionally, check out this link for a few examples of scanf: https://www.programiz.com/c-programming/c-input-output此外,请查看此链接以获取 scanf 的一些示例: https://www.programiz.com/c-programming/c-input-output

    scanf("%d", &testInteger);  

The syntax is format first, then pass in the address of where you want to store the data.语法是先格式化,然后传入要存储数据的地址。

scanf() requires a pointer to the value, it should be scanf("%d",&s1); scanf()需要一个指向值的指针,它应该是scanf("%d",&s1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM