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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:28:13+00:00 2026-06-02T15:28:13+00:00

For the following code: (assuming x has been defined) scanf(%d\n, &x); printf(foo); I expect

  • 0

For the following code: (assuming x has been defined)

scanf("%d\n", &x);
printf("foo");

I expect the program to print foo if I press 1 and ENTER, but this is not the case.
I have to press 1, ENTER, 2, ENTER to see it print foo.

Now the question is why do I need to input something plus another ENTER to make scanf return? It could’t be that scanf just need one more ENTER than in the format string because pressing 1, ENTER, ENTER won’t work either. In fact, scanf will keep waiting no matter how many ENTERs I entered until I type in something else.

I have tried this example in Visual Studio 2010 and MinGW, and they produced the same result. So how can this be explained? Is this Windows-specific? or compiler dependent? or just this kind of usage of scanf yields undefined behavior?

  • 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-02T15:28:14+00:00Added an answer on June 2, 2026 at 3:28 pm

    The '\n' in the scanf format string (as well as space, tab, '\r', '\f', and '\v') makes scanf ignore whitespace.

    ENTER is whitespace, so scanf ignores as much of it as it can and only stops when it sees something not whitespace (the 2).

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

Sidebar

Related Questions

Assuming following code. There is class MyStream witch has template overloaded operator <<. There
folks, see following code : gboolean auto_refresh_host_list(bahan *unit) { //assuming unit->setting->handle_conn has initiated and
Assuming A.sql contains the following code, then second Select query won’t be executed due
Assuming that I have the following code: final Catalog catalog = createCatalog(); for (int
Assuming a handle created with the following code: use IO::File; my $fh = IO::File->new;
Referencing the code below, I have a list of Error objects that has been
Consider the following code: public class Bar { Foo foo; void Go() { foo
Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {
Following code works perfect and adds 1 and 2 values to the list, but
Following code return error Can't convert String onto integer, please help subject = ['eng','Math','Sci']

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.