order of execution

Discussion in 'C Programming' started by John Smith, Jul 27, 2005.

  1. John Smith

    John Smith Guest

    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?
     
    John Smith, Jul 27, 2005
    #1
    1. Advertising

  2. John Smith

    Alex Fraser Guest

    "John Smith" <> wrote in message
    news:JwIFe.63903$...
    > 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
     
    Alex Fraser, Jul 27, 2005
    #2
    1. Advertising

  3. John Smith

    Barry Guest

    Try having the printf statement as the first one in the main.

    John Smith wrote:
    > 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?
     
    Barry, Jul 27, 2005
    #3
  4. John Smith wrote:
    > 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
     
    David Resnick, Jul 27, 2005
    #4
  5. "Barry" <> writes:
    > John Smith wrote:
    >> 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?

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 27, 2005
    #5
  6. John Smith

    John Smith Guest

    David Resnick wrote:
    > John Smith wrote:
    >
    >>The very first part of my program looks like this:
    >>
    >>int main()
    >>{
    >> int i=0;
    >> int j=0;
    >>
    >> printf("program starts.\n");
    >>
    >>

    >
    > 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?
     
    John Smith, Jul 29, 2005
    #6
  7. John Smith

    Michael Mair Guest

    John Smith wrote:
    > David Resnick wrote:
    >
    >>John Smith wrote:
    >>
    >>>The very first part of my program looks like this:
    >>>
    >>>int main()
    >>>{
    >>> int i=0;
    >>> int j=0;
    >>>
    >>> printf("program starts.\n");

    >>
    >>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?


    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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jul 29, 2005
    #7
  8. In article <7shGe.52583$>, John Smith <> writes:
    >
    > 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

    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
     
    Michael Wojcik, Jul 29, 2005
    #8
  9. Emmanuel Delahaye, Jul 31, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. priya
    Replies:
    0
    Views:
    1,237
    priya
    Oct 3, 2005
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Execution order of PageLoad for user controls

    =?Utf-8?B?QmlsbCBCb3Jn?=, Mar 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,352
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Mar 6, 2004
  3. Bijoy Naick

    Execution order of Validation Controls

    Bijoy Naick, Jun 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,963
    Martin Marinov
    Jun 8, 2004
  4. John Saunders

    Re: A question about order of execution?

    John Saunders, Jul 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    362
    S. Justin Gengo
    Jul 15, 2004
  5. S. Justin Gengo

    Re: A question about order of execution?

    S. Justin Gengo, Jul 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,060
    S. Justin Gengo
    Jul 15, 2004
Loading...

Share This Page