order of execution

J

John Smith

The very first part of my program looks like this:

int main()
{
int i=0;
int j=0;

printf("program starts.\n");


It then calls some functions. When the program is run, the first
message appears on the screen is not "program starts". The first
message comes from a function.

Why isn't "program starts." the first message appearing on the screen?
 
A

Alex Fraser

John Smith said:
The very first part of my program looks like this:

int main()
{
int i=0;
int j=0;

printf("program starts.\n");


It then calls some functions. When the program is run, the first
message appears on the screen is not "program starts". The first
message comes from a function.

Please provide a minimal but complete example that demonstrates the problem.

Alex
 
D

David Resnick

John said:
The very first part of my program looks like this:

int main()
{
int i=0;
int j=0;

printf("program starts.\n");


It then calls some functions. When the program is run, the first
message appears on the screen is not "program starts". The first
message comes from a function.

Why isn't "program starts." the first message appearing on the screen?

A guess, you are using a C++ compiler and have a static variable at
file
scope that is initialized with a function call. Called function
prints something? Hard to say without seeing more code. Not valid
in C, if that is the issue comp.lang.c++ is your place.

I'm assuming there aren't stupid games with macros being played
here, like
#define j j=0;call_some_func();j

There are also very platform specific possibilities, such as
user defined library initialization functions called when
libraries are loaded, if you are discussing such you should
move discussion to a group concerned with your platform...

A totally random idea is that your print to stdout is somehow buffered
(can it be past a \n?) and your function is printing to stderr.
Doubt it.

-David
 
K

Keith Thompson

Barry said:
Try having the printf statement as the first one in the main.

Please don't top-post. Your response belongs below any quoted text,
not above it. (I'm not too upset; a lot of groups.google.com users
don't provide any quoted text at all.)

Your suggestion is unlikely to help. The printf() call already is the
first statement in main(); the preceding lines are declarations. In
C90, statements cannot precede declarations within a block; though
it's allowed in C99. Finally, the declaration and initialization of i
and j cannot produce any output.

My best guess is that something else is writing to stderr, which is
unbuffered, but there's not enough information to be sure.
 
J

John Smith

David said:
A totally random idea is that your print to stdout is somehow buffered
(can it be past a \n?) and your function is printing to stderr.
Doubt it.


So true. After re-examining the codes, I found that those output
which appeared before "program starts." were generated by
"fprintf(stderr,..)" in functions called by main().

What gives?
 
M

Michael Mair

John said:
So true. After re-examining the codes, I found that those output
which appeared before "program starts." were generated by
"fprintf(stderr,..)" in functions called by main().

What gives?

You can either redirect stderr or stdout or -- if you really want
to mix error messages and normal messages, you can fflush(stdout);
if messages to stderr happen on a regular base, I would rather think
about another output strategy.
Another way might be to use setbuf()/setvbuf() in main() before any
other stream operation to change the buffering behaviour; I have never
used either on one of the default streams but the description does
not indicate that it is not guaranteed to work with one of them.


Cheers
Michael
 
M

Michael Wojcik

So true. After re-examining the codes, I found that those output
which appeared before "program starts." were generated by
"fprintf(stderr,..)" in functions called by main().

What gives?

stdout and stderr are different streams. They are not required to
synchronize their output (which may be directed to different
destinations; the Standard certainly allows that). They are not
required to have the same buffering mode. In particular:

When opened, the standard error stream is not fully buffered;
the standard input and output streams are fully buffered if
and only if the stream can be determined not to refer to an
interactive device. (C90 7.9.3)

C defines three buffering modes for streams: unbuffered, line
buffered, and fully buffered. The last cannot be used for stderr,
which leaves the implementation two options. It can (and must) only
be used for stdin and stdout if the implementation knows that they're
not connected to an interactive device; that also leaves two options,
if they *are* connected to an interactive device.

Buffering modes can be changed with the setbuf and setvbuf functions.

OT: Typically, for Unix-derived implementations, stderr is unbuffered
and stdout is line-buffered, when they're connected to a tty. When
stdout is not connected to a tty, it must be fully buffered. Thus,
for example, on one implementation:

$ cat testc.c
#include <stdio.h>
int main(void)
{
fputs("from stdout\n", stdout);
fputs("from stderr\n", stderr);
return 0;
}

$ ./testc
from stdout
from stderr

$ ./testc | cat
from stderr
from stdout

In the second case, piping stdout means it must be fully-buffered, so
its output appears only when the buffer is full or the stream is
flushed, as it is when it's implicitly closed at program termination.

Stream buffering is not the whole story, however. What happens when
the C library passes outbound data to the environment is an
implementation and environmental issue.

If you're concerned about only this one line appearing first, you
could try fflush(stdout); it's not guaranteed to have the effect you
want, but it's a start.

If in general you want output to stdout and stderr to happen in the
order in which your program performs it, you could try setting them
both to be unbuffered. Again, there are no guarantees.

--
Michael Wojcik (e-mail address removed)

However, we maintain that our mission is more than creating high-tech
amusement--rather, we must endeavor to provide high-tech, high-touch
entertainment with an emphasis on enkindling human warmth.
-- "The Ultimate in Entertainment", from the president of video game
producer Namco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top