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?
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:
Output: