Search code examples
cregexscanfidentifier

Reading an Identifier into a C Program using scanf()


I need my C program to be able to read in an identifier using the scanf() method in C.

An identifier in this case is a letter or a _ character followed by one or more alphanumeric characters including the _ character.

The regular expression would be

    [a-ZA-Z_][a-zA-Z0-9_]*

These are examples of correct identifiers:

    _identifier1
    variable21

These are examples of incorrect identifiers

    12var
    %foobar

Does anybody know how this would be done using scanf() in C?


Solution

  • scanf() doesn't support regular expressions. There's no regular expression support at all in the standard C library. You'll have to read a string and then parse it "manually".

    For example:

    #include <stdio.h>
    #include <ctype.h>
    
    int isIdentifier(const char* s)
    {
      const char* p = s;
    
      if (!(*p == '_' || isalpha(*p)))
      {
        return 0;
      }
    
      for (p++; *p != '\0'; p++)
      {
        if (!(*p == '_' || isalnum(*p)))
        {
          return 0;
        }
      }
    
      return 1;
    }
    
    int main(void)
    {
      const char* const testData[] =
      {
        "a",
        "a_",
        "_a",
        "3",
        "3a",
        "3_",
        "_3"
      };
      int i;
    
      for (i = 0; i < sizeof(testData) / sizeof(testData[0]); i++)
      {
        printf("\"%s\" is %san identifier\n",
               testData[i],
               isIdentifier(testData[i]) ? "" : "not ");
      }
    
      return 0;
    }
    

    Output:

    "a" is an identifier
    "a_" is an identifier
    "_a" is an identifier
    "3" is not an identifier
    "3a" is not an identifier
    "3_" is not an identifier
    "_3" is an identifier