I wrote a code in C language to print passed/not passed on basis of his marks. No matter, what I input, I am getting the same results i.e passed

Viewed 61
# include<stdio.h>

int main() {
    
    int marks;
    printf("enter marks: ");
    scanf("enter marks: %d ", &marks);

    if(marks > 30)
    {
        printf("passed");

    }

    else
    {
        printf("not");
    }
    
    return 0;

}

The criteria that I put doesn't help in the output, even if enter marks as 14, then too it displays passed. I am a beginner, so please help.

2 Answers
printf("enter marks: ");
scanf("enter marks: %d ", &marks);

Is not correct.

You do not put a "prompt" in scanf. Only the value you want to read.

It should be:

scanf("%d", &marks);  // Prompt was already done with printf on prior line
#include <stdio.h>
#include <conio.h>

int main() {
    
    int marks;
    printf("enter marks:");
    scanf("%d",&marks);

    if(marks > 30)
    {
        printf("passed");

    }

    else
    {
        printf("not");
    }
    
    return 0;

}

its working fine

Related