简体   繁体   中英

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:

这是代码的输出

I am trying to make a percentage calculator and it shows a weird 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. Otherwise, scanf will not behave as you expect.

Right now, you are not passing in the address to 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

I recommend reading a little bit about how scanf works at the following link.

"Following is the declaration for scanf() function.

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("%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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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