Ayush panwar
Ayush panwar

Reputation: 21

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:

this is output of code

I am trying to make a percentage calculator and it shows a weird output.

What am I doing wrong?

Upvotes: 0

Views: 194

Answers (2)

HereForTheCode
HereForTheCode

Reputation: 17

scanf() requires a pointer to the value, it should be scanf("%d",&s1);

Upvotes: 1

Kelsey
Kelsey

Reputation: 483

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.

Upvotes: 1

Related Questions