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

  • SEARCH
  • Home
  • 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 8559617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:05:41+00:00 2026-06-11T16:05:41+00:00

I’m trying to parse xxxxxx(xxxxx) format string using sscanf as following: sscanf(command, %s(%s), part1,

  • 0

I’m trying to parse xxxxxx(xxxxx) format string using sscanf as following:

sscanf(command, "%s(%s)", part1, part2)

but it seems like sscanf does not support this format and as a result, part1 actually contains the whole string.

anyone has experience with this please share…

Thank you

  • 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-06-11T16:05:43+00:00Added an answer on June 11, 2026 at 4:05 pm

    Converting your code into a program:

    #include <stdio.h>
    
    int main(void)
    {
        char part1[32];
        char part2[32];
        char command[32] = "xxxxx(yyyy)";
        int n;
    
        if ((n = sscanf(command, "%s(%s)", part1, part2)) != 2)
            printf("Problem! n = %d\n", n);
        else
            printf("Part1 = <<%s>>; Part2 = <<%s>>\n", part1, part2);
        return 0;
    }
    

    When run, it produces ‘Problem! n = 1‘.

    This is because the first %s conversion specifier skips leading white space and then scans for ‘non white-space’ characters up to the next white space character (or, in this case, end of string).

    You would need to use (negated) character classes or scansets to get the result you want:

    #include <stdio.h>
    
    int main(void)
    {
        char part1[32];
        char part2[32];
        char command[32] = "xxxxx(yyyy)";
        int n;
    
        if ((n = sscanf(command, "%31[^(](%31[^)])", part1, part2)) != 2)
            printf("Problem! n = %d\n", n);
        else
            printf("Part1 = <<%s>>; Part2 = <<%s>>\n", part1, part2);
        return 0;
    }
    

    This produces:

    Part1 = <<xxxxx>>; Part2 = <<yyyy>>
    

    Note the 31’s in the format; they prevent overflows.


    I’m wondering how does %31 works. Does it work as %s and prevent overflow or does it just prevent overflow?

    With the given data, these two lines are equivalent and both safe enough:

        if ((n = sscanf(command, "%31[^(](%31[^)])", part1, part2)) != 2)
        if ((n = sscanf(command, "%[^(](%[^)])", part1, part2)) != 2)
    

    The %[...] notation is a conversion specification; so is %31[...].

    The C standard says:

    Each conversion specification is introduced by the character %.
    After the %, the following appear in sequence:

    • An optional assignment-suppressing character *.
    • An optional decimal integer greater than zero that specifies the maximum field width
      (in characters).
    • An optional length modifier that specifies the size of the receiving object.
    • A conversion specifier character that specifies the type of conversion to be applied.

    The 31 is an example of the (optional) maximum field width. The [...] part is a scanset, which could perhaps be regarded as a special case of the s conversion specifier. The %s conversion specifier is approximately equivalent to %[^ \t\n].

    The 31 is one less than the length of the string; the null at the end is not counted in that length. Since part1 and part2 are each an array of 32 char, the %31[^(] or %31[^)] conversion specifiers prevent buffer overflows. If the first string of characters was more than 31 characters before the (, you’d get a return value of 1 because of a mismatch on the literal open parenthesis. Similarly, the second string would be limited to 31 characters, but you’d not easily be able to tell whether the ) was in the correct place or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.