Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:07:33+00:00 2026-05-27T06:07:33+00:00

I need my C program to be able to read in an identifier using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:07:33+00:00Added an answer on May 27, 2026 at 6:07 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to make a program that looks through a mailbox
I have a program that I need to be able to search a file
I'm doing a theatrical performance, and I need a program that can read images
I need to create program that creates a XML schema like below using System.Xml.XmlSchema
I'm writing a C++ program where I need to be able to parse C
I'm writing this program that will need to access the registry to pull some
I need to write a simple terminal-based program that should, Read some text from
Is there a way to read the web service methods dynamically using a program?
Ok, so I'm building this program in C for a Linux system. I need
I need to make a program in C++ that must read and write text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.