Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can any one help me to understand this c code????

Status
Not open for further replies.

raj_rohit10

Advanced Member level 4
Joined
Jul 14, 2004
Messages
100
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
891
The below given code prints only hello-err for some times then in bulk it prints hello-out...
what is the reason behind this?If i am adding \n in fprintf then it works fine...

#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{

fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
output of this code is On Solaris-Sparc Machine : hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-
errhello-errhello-errhello-errhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-outhello-
outhello-outhello-out
While as per c it should be :hello-outhello-errhello-outhello-errhello-outhello-errhello-outhello-err...
for reply thanks in advance
rohit raj
 

This is because stdout is buffered. You can force the stdout stuff to be printed immediately by using fflush();

See "man fflush" for details...
 

but can u explain me why this problems occurs when it is used with stderr??
 

You are writing to two different output streams. C does not dictate how streams should buffer their data. Many implementations choose no buffering on stderr and line buffering on stdout. Your stdout seems to have a 1024 byte line buffer, so it will output your infinitely long line in 1024 byte chunks. If you want to intermingle stderr and stdout, then use fflush() or \n to get sensible order.
 
is there any way for compilation which will remove buffering for the stdout ??
 

Assigns buffering to a stream

setbuf and setvbuf functions cause the buffer buf to be used for I/O buffering
instead of an automatically allocated buffer.

void setbuf(FILE *stream, char *buf);
int setvbuf(FILE *stream, char *buf, int type, size_t size);

With these functions, you can change size of buffer associated to a file stream and change type of buffering.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top