[comp.lang.c] execution out of order ?

jdm5548@diamond.tamu.edu (James Darrell McCauley) (07/19/90)

What generally causes instructions to be executed out of order?
This occurred once while over stepping the bounds of an array
(and I assume writing in memory locations where instructions are
stored), but I don't believe that is the case for my current
problem.  Specifically, what would cause the following?

  /* instructions */                   /* script of execution */
  main()                                % gcc main.c
  {                                     % a.out
     ...                                *Christ
     fgetc(stdin);                      *comes
     printf("*Christ\n");               ^M^M
     ...                                *first
     printf("*comes\n");                %
     fgetc(stdin);                      % <bang head against console>
     printf("*first\n");
  }

I get the same results with getchar().
This may be a FAQ and/or reflect my own ignorance, so in an effort to
avoid negative comments, please E-mail any suggestions. Thanks.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James Darrell McCauley                 jdm5548@diamond.tamu.edu
Dept of Ag. Engineering                          (409) 845-6484
Texas A&M University, College Station, Texas 77843-2117, USA
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

jdm5548@diamond.tamu.edu (James Darrell McCauley) (07/21/90)

I'm an reposting this question because, as 1 of the 2 responses
mentioned, I may have not made myself quite clear.
Here's the code fragment:
	main()
	{
  	  ...
          getchar();  /* stdin, as well as stdout, are console */
          printf("first line\n");
          ...  /* calculations - no I/O */
          printf("second line\n");
          getchar();
          printf("third line\n");
        }
and script of execution:
        % a.out
        first line
        second line
        <hit return>
        <hit return again>
        third line
        %
I should have to press return before the first line is printed! Why does it do
this? Do I have to clear the input buffer somehow?
I am running SunOS 4.0.3 and I am not redirecting stdin or stdout.
I have experienced the same thing using DOS.  This is just a general question
(not something I couldn't work around) that I don't think should be machine
specific.

Thanks.
Darrell McCauley, jdm5548@diamond.tamu.edu (128.194.14.1)
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James Darrell McCauley                 jdm5548@diamond.tamu.edu
Dept of Ag. Engineering                          (409) 845-6484
Texas A&M University, College Station, Texas 77843-2117, USA
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cpcahil@virtech.uucp (Conor P. Cahill) (07/21/90)

In article <6732@helios.TAMU.EDU> jdm5548@diamond.tamu.edu (James Darrell McCauley) writes:
>I'm an reposting this question because, as 1 of the 2 responses
>mentioned, I may have not made myself quite clear.
>Here's the code fragment:

When you post a message with a code fragment, post the entire fragment that
exhibits the indicated behavior.  I ran you fragment (deleting the elipsisi)
and it acted as on would expect.  I don't know what the missing code is,
but it probably has something to do with the problem.

>	main()
>	{
>  	  ...
>          getchar();  /* stdin, as well as stdout, are console */
>          printf("first line\n");
>          ...  /* calculations - no I/O */
>          printf("second line\n");
>          getchar();
>          printf("third line\n");
>        }

So show us the missing code or post an exact copy of the code that
presents the problem.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170