0

This is not so much a coding question but a syntax question for C. Consider this simple function that checks whether a user input character is a number or operator, assuming that the user only inputs either numbers or operator symbols:

int isnumber(char c)
{
    if ((c == '+') || (c == '-') || (c == '*') || (c == '/') || (c == '(') || (c == ')'))
    {
        return 0;
    }
    return 1;

}

Very frequently, I find myself needing to do boolean checks like this for whatever reason, and I find it a pain to write (c == something) separated by boolean operators everytime. So I was wondering if there was an easier, shorter version to write something like this in the syntax for C Programming?

Ymi
  • 609
  • 6
  • 18
  • 2
    What about just calling `isdigit`? Or `int isnumber(char c) { return c >= '0' && c <= '9'; }` – paddy Apr 20 '21 at 03:02
  • @paddy I wasn't aware of that, thanks for the tip. But the crux of the question isn't the function itself, it's about asking if there's a shorter way to write if statements with boolean expressions. – Ymi Apr 20 '21 at 03:04
  • What about: `int isoperator(char c) { return c && strchr("+-*/()", c); }` – paddy Apr 20 '21 at 03:05

0 Answers0