[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
[LI] Re: [Linux India Digest V1 #300]
Date: Wed, 3 Nov 1999 23:12:02 +0530
From: Mrinal Kalakrishnan <mrinal@xxxxxxxxx>
Subject: [LI] C Programming problem
Hi,
This particular piece of code behaves differently when compiled with gcc
(Linux), when compared to Turbo C or DJGPP (in DOS).
#include <stdio.h>
void main()
{
printf("Loading..");
float i,j;
j=12345.6789;
for (i=0.0;i<50000.0;i+=0.01) j=j*j;
printf("Done\n");
}
The expected behaviour would be that "Loading" gets printed, then a delay,
and
then "Done" gets printed. But, that doesn't happen. Instead, there is a long
delay initially, then "loading" and "done" get printed immediately one after
the other. However, if I add a newline in the "Loading" string, then it gets
printed immediately, followed by the delay.
Anybody faced this problem before - any solutions?
- ----------------------------------------------------------------
Mrinal Kalakrishnan
mrinal@xxxxxxxxx
http://listen.to/mrinal
- ----------------------------------------------------------------
- --------------------------------------------------------------------
mrinal, first of all this code is supposed to give compilation error in gcc.
reason : whenever you declare a block you need to define the variable first
then you ask for a function. your block started immedietly after main(). this
is ANSI specification. so you may have got some difference in tc and gcc.
next , printf is buffered. if you have used a fflush(stdout)(same effect of
putting a \n) after printf then the buffer would have been cleared and you
would have got your expected output. otherwise u can try unbuffered write().
DS
____________________________________________________________________
Get your own FREE, personal Netscape WebMail account today at http://webmail.netscape.com.
--------------------------------------------------------------------
The Linux India Mailing List Archives are now available. Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.