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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:42:00+00:00 2026-06-02T03:42:00+00:00

I wrote the following code: int N; scanf(%d, &N); int i; for (i =

  • 0

I wrote the following code:

int N;
scanf("%d", &N);
int i;
for (i = 0; i < N; i++) {
  char line[LINE_MAX];
  if (fgets(line, LINE_MAX, stdin) != NULL) {
    // do stuff with line here
    printf("%c - %c\n", line[0], line[1]);
  }
}

I have an input file which has the number of lines it has, and then that number of lines followed which I want to process. So I read in the number of lines into N. After that, I use fgets to get the line to be able to process it.

However, fgets does not seem to wait for a stdin the first time. I always get output of -, and then it waits for input. Meaning, the first iteration of the loop, it is not waiting for standard input at fgets and just prints out two empty characters separated by - as my printf does.

Why is that? How can I get fgets to wait for input each time? I feel like it is a threading issue.

  • 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-02T03:42:03+00:00Added an answer on June 2, 2026 at 3:42 am

    As geekosaur said, you are not handling the newline left behind by scanf. You can modify your scanf format string to take it into account:

    scanf("%d *[^\n]", &N);
    

    *[^\n] says to ignore everything after your integer input that isn’t a newline, but don’t do anything with the newline (skip it).

    Test program output:

    emulawsk@cs:~/testing$ ./test2
    3
    13
    1 - 3
    26
    2 - 6
    59
    5 - 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following code: void buildArrays(char *pLastLetter[],int length[], int size, const char str[])
Using the following code to write to a file int main(int argc, char *
i wrote the following code.... #include< iostream> #include< fstream> using namespace std; int main()
I wrote the following code in a JSP file: <c:catch var=e> <% int accountNumber
I made a simple experiment, I wrote the following code in Xcode: int main(int
I wrote the following code snippet: void foo() { struct _bar_ { int a;
While working with pointers i wrote the following code, int main() { int a[]={10,20,30,40,50};
I have wrote the following code: int fd = _dup(fileno(stdout)); FILE* tmp = freopen(tmp,w+,stdout);
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':

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.