1
#include <stdio.h>

int main()
{
    int a, b, c;

    /* Input two numbers from user */
    printf("Enter two numbers to find maximum: ");
    scanf("%d%d", &a, &b);
    c = a > b;
    switch (c)
    {   
      case 0:
        printf("%d is maximum", b);
        break;

      case 1:
        printf("%d is maximum", a);
        break;

      default:
        printf("Invalid Input");
    }

    return 0;
}

I want to print default statement in this C program by entering wrong input like float or character const. Whenever I enter any char type variable or floating point number this happens

Output example 1:

Enter two numbers to find maximum: 2.5
509 is maximum

Output example 2:

Enter two numbers to find maximum: g
512 is maximum

Expected Output should be:

Enter two numbers to find maximum: g
Invalid Input

Expected Output should be:

Enter two numbers to find maximum: 22.6
Invalid Input
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Mazin
  • 21
  • 1
  • 1
    In this example, this is impossible to enter in default case. Because a > b always returns 0 or 1. If you want to check the scanf result, you can have a look at this question: https://stackoverflow.com/questions/8186115/how-can-i-determine-if-scanf-read-what-was-specified-in-format – senerh Aug 15 '23 at 09:38

2 Answers2

2

Validating the input and evaluating the expression should be done in 2 separate phases:

#include <stdio.h>

int main(void)
{
    char buf[80];
    char eol;
    int a, b;

    /* Input two numbers from user */
    printf("Enter two numbers to find maximum: ");
    if (!fgets(buf, sizeof buf, stdin)) {
        printf("no input\n");
        return 1;
    }
    /* input should be 2 integers and some white space */
    if (sscanf(buf, "%d%d %c", &a, &b, &eol) != 2) {
        printf("invalid input: %s\n", buf);
        return 1;
    }
    if (a > b) {
        printf("%d is maximum\n", a);
    } else {
        printf("%d is maximum\n", b);
    }
    return 0;
}

If you insist on storing the state in variable c, you could do this:

#include <stdio.h>

int main(void)
{
    char buf[80];
    char eol;
    int a = 0, b = 0, c = -1;

    /* Input two numbers from user */
    printf("Enter two numbers to find maximum: ");
    if (fgets(buf, sizeof buf, stdin)) {
        /* input should be 2 integers and some white space */
        if (sscanf(buf, "%d%d %c", &a, &b, &eol) == 2) {
            c = a > b;
        }
    }
    switch (c) {
    case 0:
        printf("%d is maximum\n", b);
        break;
    case 1:
        printf("%d is maximum\n", a);
        break;
    default:
        printf("invalid input\n");
        break;
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

c = a > b; can in theory only ever be 1 or 0.

What you actually want is probably this:

c = scanf("%d%d", &a, &b);

switch(c)
{
  case 2: 
    /* expected */
    /* additional error handling of inputs here if needed */
    break;

  default:
    /* scanf failed */
}
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
Lundin
  • 195,001
  • 40
  • 254
  • 396