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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:34:26+00:00 2026-05-26T18:34:26+00:00

I was asked to do a work in C when I’m supposed to read

  • 0

I was asked to do a work in C when I’m supposed to read from input until there’s a space and then until the user presses enter.
If I do this:

scanf("%2000s %2000s", a, b);

It will follow the 1st rule but not the 2nd.
If I write:

I am smart

What I get is equivalent to:
a = “I”;
b = “am”;
But It should be:
a = “I”;
b = “am smart”;

I already tried:

scanf("%2000s %2000[^\n]\n", a, b);

and

scanf("%2000s %2000[^\0]\0", a, b);

In the 1st one, it waits for the user to press Ctrl+D (to send EOF) and that’s not what I want.
In the 2nd one, it won’t compile. According to the compiler:

warning: no closing ‘]’ for ‘%[’ format

Any good way to solve this?

  • 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-26T18:34:27+00:00Added an answer on May 26, 2026 at 6:34 pm

    scanf (and cousins) have one slightly strange characteristic: white space in (most placed in) the format string matches an arbitrary amount of white space in the input. As it happens, at least in the default "C" locale, a new-line is classified as white space.

    This means the trailing '\n' is trying to match not only a new-line, but any succeeding white-space as well. It won’t be considered matched until you signal the end of the input, or else enter some non-white space character.

    One way to deal with that is something like this:

    scanf("%2000s %2000[^\n]%c", a, b, &c);
    
    if (c=='\n')
        // we read the whole line
    else
        // the rest of the line was more than 2000 characters long. `c` contains a 
        // character from the input, and there's potentially more after that as well.
    

    Depending on the situation, you might also want to check the return value from scanf, which tells you the number of conversions that were successful. In this case, you’d be looking for 3 to indicate that all the conversions were successful.

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

Sidebar

Related Questions

I know this exact question was asked here , but the answer didn't work
I was asked to work on this back-end scheduled job that export some customers
A friend of mine (from work) asked me a question. Remove the static keyword
@Spence asked this Previous Question . So, how's that work in Java? Generic types
I have been asked this question in interview how does XML and Soap work
Okay so I have the sql to work this out as asked in the
This was asked in another question, but none of the solutions appear to work
I got a MacMini at work for development, and was asked to follow this
I asked for a 'work' permission and got this response.work array of all current
I know this question has been asked before but the other solutions didn't work

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.