Mrinal Kalakrishnan forced the electrons to say:
> 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.
> 

This is one of the most common problems faced by people who are new to
the bufering in the stdio library of Unix. In your case, the library
buffers the string "Loading..." waiting for more data to output (maybe
via further printf() or puts() or fputs() or fputc() or whatever calls)
until the buffer is full, or the stream is closed or a newline is
received. Then the entire buffer is flushed, and the buffered data
actually appears in the stream (here, stdout).

One solution is to force the buffer to be flushed after the first printf()
call, using the fflush() function of the stdio library. Another solution
is to turn off buffering in the stdout stream (this is not recommended).

As an aside, standard C demands that main should always return an
int. Your program might crash if you declare main as void main().

Binand

-- 
#include <stdio.h>                                   | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c;     | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c";    | printing program.
int main(){printf(p,10,34,p,34,10);return 0;}        | Try it!!
--------------------------------------------------------------------
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.

Reply via email to