[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Printf function not printing sting.

Posted on 2009-04-27
11
Medium Priority
?
981 Views
Last Modified: 2013-12-06
Hi
     I wrote a simple piece of code to print  "Hello" , "world" with sleep between two printf statement,according to the code it should print "Hello" then go for 10 sec sleep and then print "world".
But it display "HelloWorld" as output after 10 sec sleep.
when i debug also it is not printing as, printf statement gets executed.it display output when program complelely executes.

I am using SUSE Linux 11 with kernel version
Linux  2.6.25.5-1.1-default #1 SMP 2008-06-07 01:55:22 +0200

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
        printf("Hello");
        sleep(10);
        printf("World");
        return 0;
}

Open in new window

0
Comment
Question by:ajit007
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24244670
fflush(stdout)
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24244687
Hi ajit,

This is likely due to output being buffered.  The text is being written to stdout instead of directly to the console.

Put an fflush (stdout) before the sleep() statement.  For consistency, you should also printf (stdout, ...).  (Unless you need to write directly to the console device.


Good Luck,
Kent
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24244695
You need to put a live feed as the end to flush the output or, as ozo has shown, explicitly flush,

print("hello\n")
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 40

Expert Comment

by:evilrix
ID: 24244742
>> live feed
Line feed :)
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 24258797
Hello

I hate printf in linux. I suggest you use printw
regards
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
printw("this is a test");
return 0;
}

Open in new window

0
 

Author Comment

by:ajit007
ID: 24263864
Why we need to add fflush ??
0
 
LVL 85

Expert Comment

by:ozo
ID: 24263948
stdout is probably connected to a line buffered stream
http://www.gnu.org/software/hello/manual/libc/Buffering-Concepts.html
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24263956
>> Why we need to add fflush ??
You shouldn't need to flush if you ensure the string ends with a linefeed "\n". The problem is the standard console doesn't actually print to the screen until either the data sent to it has been flushed or until it receives a new line character. The flush is just forcing what a new line will do anyway, but it's good to use if you data doesn't contain a newline.
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 200 total points
ID: 24264115
Hi ajit,

You may find it interesting (and for the long term it will be necessary) to see how the behavior of the standard streams differ.

I've modified your small program to write explicitly to stdout instead of the console.  And modified that to write to stderr.  Give them a try.  :)

Note that when writing to stdout you get the exact behavior that you described in your original post, but when writing to stderr you get the behavior that you wanted.  That's because stderr does not buffer data between writes.  (It's not a good idea to write to stderr just to get this effect.)

You can make the stdout stream behave exactly like the stderr stream just by flushing the buffer after every write to stdout.


Kent

//
//  Write "Hello World" to stdout
//
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
        fprintf(stdout, "Hello");
        sleep(4);
        fprintf(stdout, " World\n");
        return 0;
}
 
 
 
//
//  Write "Hello World" to stderr
//
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
 
        fprintf(stderr, "Hello");
        sleep(4);
        fprintf(stderr, " World\n");
        return 0;
}

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 24264218
>  For consistency, you should also printf (stdout, ...).
printf( format, ... ) is equivalent to fprintf( stdout, format, ... )
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 24534708
If you don't want to use fflush() calls, you can probably remove line bufferization by call to:
  setvbuf(stdout, NULL, _IONBF, 0);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you sitting there reading this and wondering how to get started with Linux? It almost seems like picking the right Linux distribution is about like picking the right college or buying a new car if you read some of the article out there. Relax… l…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question