Home > OS >  When i put an input like "a", it does not print out the output "The value of n is uni
When i put an input like "a", it does not print out the output "The value of n is uni

Time:05-16

#include <stdio.h>

 int main(){
    int m;

    printf("Enter the value of m: ");
    scanf("%d", &m);

    if(m > 0){
        printf("The value of n = 1");
    }
    else if(m == 0){
        printf("The value of n = 0");
    }
    else if(m < 0){
        printf("The value of n = -1");
    }
    else{
        printf("The value of n is unindentified");
    }
    return 0;
    }

Write a C program to read the value of an integer m and display the value of n is 1 when m is larger than 0, 0 when m is 0, and -1 when m is less than 0.

CodePudding user response:

That is because after int m; m will have a value in it (it can be either 0 or a random number). Since int is an integer it can never be anything other than a number. You are checking if it is equal to 0, less, or greater than 0 and with that, you have covered all the cases. Last else will never happen

CodePudding user response:

In a statically typed language such as C, an integer variable (m in your code) will stay an integer until the end of the world (or until you change your code). Therefore m can't be anything else than a number > 0, number < 0 or a number equal to 0. Your else part is ureachable and you can safely remove it.

  • Related