LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2020, 03:50 PM   #31
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled

It's just a guess since you didn't post the version that didn't work but if you didn't wrap the functions in a blocl it would exhibit the behavior described.
i.e.
Code:
for (i = 0; i < 10 ; i++)
  printf("enter a number: \n"); scanf("%d", &a[i]);
copy and paste this to your favorite editor, compile and see if it works.
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
    
    int i;
    int a[10] = {0};

    for (i = 0; i < 10; i++) {
        printf ("enter number %d:", i);
	fflush (stdout);
	scanf ("%d", &a[i]);
    }

    for (i = 0; i < 10; i++) {
    	printf ("%d: %d\n", i, a[i]);
    }

    return 0;

}
On my system it works without flushing stdout but GazL is correct in saying it's the right thing to do. In your example the \n will flush the stream.

Last edited by SoftSprocket; 08-29-2020 at 03:51 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration