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 8074467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:39:30+00:00 2026-06-05T14:39:30+00:00

Here i have one character arrray in which there are some splitters are there.

  • 0

Here i have one character arrray in which there are some splitters are there.

character array is char sentence []="abc_12.22.32.42";

I want to read abc,12,22,32,42 from the sentence using sscanf.

I tried with sscanf but its given different output.

I posted here two code one of its working fine.

if i read abc = 12 with %s = %s then its works. Why we have to give space between splitter.

Any way to achieve this goal.

Not working Code :

#include <stdio.h>

int main ()
{
  char sentence []="abc_12.22.32.42";
  char str [20];
  char str1 [20];
  char str2 [20];
  char str3 [20];
  sscanf (sentence,"%s_%s.%s.%s.%s",str,str1,str2,str3);
  printf ("%s --> %s --> %s --> %s --> %s\n",str,str1,str2,str3);
  return 0;
}

Output :

abc_12.22.32.42-->  -- � -->�Tl�s --> (null)

Expected Output :

 abc --> 12 --> 22 --> 32 --> 42

working Code :

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="abc = 12";
  char str [20];
  char str1 [20];
  char str2 [20];
  char str3 [20];
  if(sscanf(sentence,"%s = %s",str,str1) < 5) ;
        printf ("%s--> %s\n",str,str1);
  return 0;
}

output :

abc--> 12
  • 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-05T14:39:31+00:00Added an answer on June 5, 2026 at 2:39 pm

    When specifying format like this: %s_%s.%s.%s.%s, your program can’t know that first string can’t contain characters '_' and '.'. That’s the reason why sscanf(sentence,"%s_%s.%s.%s.%s", str0, str1, str2, str3, str4); causes whole sentence to be stored in str0.

    If you want to stay with sscanf, you’ll have to change the format that you expect:

    char sentence[] = "abc_12.22.32.42";
    char str0[20], str1[20], str2[20], str3[20], str4[20];
    sscanf (sentence,"%19[^_]_%19[^.].%19[^.].%19[^.].%19[^.]",
            str0, str1, str2, str3, str4);
    printf ("%s --> %s --> %s --> %s --> %s\n",
            str0, str1, str2, str3, str4);
    

    %19[^_] causes str0 to contain max 19 characters (20 with '\0') and prevents character '_' to become part of str0. Thus the output becomes abc --> 12 --> 22 --> 32 --> 42 as desired.

    Alternatively you could use strtok.

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

Sidebar

Related Questions

Here i want to do UDP Socket programming in C. I have one device
Ok, so here's what I want to do, preferably with jQuery 1.4.2: Have one
Here's the situation I have a webpage which has one drop down called prefer.
here i have one function which is call many times in one application this
Here is my situation: I have one table that contains a list of drugs
Here is the situation: You have one long-running calculation running in a background thread.
so here's the problem I have one form, it outputs search results after submit,
here is the problem. I have one PC using VS2008 (SP1 ) and another
Here, the Sencha team explains how to have a one to many relationship: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store
Here's another one for you to help me solve: I have an ASP.NET website

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.