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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:17:29+00:00 2026-06-18T06:17:29+00:00

I’m doing homework that asks me to read an integer n representing the size

  • 0

I’m doing homework that asks me to read an integer n representing the size of a loop and then read a line of characters n times and print it right after the user’s input. So I used scanf and then I print it with printf. The problem is that if the user’s input is only a newline character, it should print another \n, but scanf seems to ignore the input when it’s a single \n.

Is there any way to make this assignment with scanf or should I try something else?

int i;
scanf("%d", &i);
for(int ct=0; ct<i; ct++)
{
    char buff[28];
    scanf("%s", buff); // if I just press enter here
    prtinf("%s\n", buff); // then I must get \n\n here
}
  • 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-18T06:17:30+00:00Added an answer on June 18, 2026 at 6:17 am

    Using fgets to read in a line is simpler and more robust:

    if (!fgets(buff, 28, stdin))
    {
        // reading failed, do appropriate error handling
        // we're just exiting here
        exit(EXIT_FAILURE);
    }
    // We have successfully read in a line, or at least the first 27
    // characters of the line. Check whether a full line was read,
    // if it was, whether the line was empty
    size_t l = strlen(buff);    // <string.h> must be included
    if (buff[l-1] == '\n')
    {
        // a full line was read, remove trailing newline unless
        // the line was empty
        if (l > 1)
        {
            buff[l-1] = 0;
        }
    }
    else
    {
        // the input was too long, what now?
        // leave the remaining input for the next iteration or
        // empty the input buffer?
    }
    printf("%s\n",buff);
    

    It doesn’t work with scanf("%s",buff) because most scanf conversions ignore leading white space:

    Input white-space characters (as specified by the isspace function) are skipped, unless
    the specification includes a [, c, or n specifier.

    So if the user inputs an empty line, scanf ignores that input unless its format is one of the exceptional.

    You can use scanf with a character set format instead,

    scanf("%27[^\n]%*c", buff);
    

    to read all characters until a newline (but limited to 28 - 1 here to avoid buffer overruns), and then consume a newline without storing it (the * in the %*c conversion specifier suppresses assignment), that would handle non-empty lines consisting entirely of whitespace, which the %s conversion would not. But if the first character of the input is a newline, the %27[^\n] conversion fails (thanks to chux for drawing attention to that), the newline is left in the input buffer, and subsequent scans with that format would also fail if the newline isn’t removed from the input buffer.

    A somewhat robust (but ugly; and not dealing with too long input) loop using scanf would, as far as I can see, need to check for a newline before scanning, e.g.

    for(int ct = 0; ct < i; ++ct)
    {
        int ch = getchar();
        if (ch == EOF)
        {
            // something bad happened; we quit
            exit(EXIT_FAILURE);
        }
        if (ch == '\n')
        {
            // we had an empty line
            printf("\n\n");
        }
        else
        {
            // The first character was not a newline, scanning
            // with the character set format would have succeeded.
            // But we don't know what comes next, so we put the
            // character back first.
            // Although one character of pushback is guaranteed,
            if (ungetc(ch,stdin) == EOF)
            {
                // pushback failed
                exit(EXIT_FAILURE);
            }
            scanf("%27[^\n]%*c",buff);
            printf("%s\n",buff);
        }
    }
    

    Use fgets, really. It’s better.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

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.