Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple function calls inside a while loop question

1 view
Skip to first unread message

Chad

unread,
Apr 4, 2010, 2:10:57 PM4/4/10
to
Let's say I have the following...

#include <stdio.h>

void foo(void)
{
printf("I'm a function call inside of foo\n");
}

void bar(void)
{
printf("I'm a function call inside of bar\n");
}

int main(void)
{
int x = 1;

while(x) {
foo();
bar();
}

return 0;
}

What prevents bar() from executing before foo() is finished in this
while loop?

Chad

Malcolm McLean

unread,
Apr 4, 2010, 4:10:52 PM4/4/10
to
On 4 Apr, 19:10, Chad <cdal...@gmail.com> wrote:
> Let's say I have the following...
>
> What prevents bar() from executing before foo() is
> finished in this
> while loop?
>
C is a serial language. Ater each sequence point (roughly, semicolon)
the state of the program has to be updated.
Since the functions call printf the situation is a bit more
complicated thna that on many systems, since printf() quite frequently
passes data to another process which produces the actual visible
output. However the stream is synchronised so later calls never
overtake earlier ones.

lawrenc...@siemens.com

unread,
Apr 4, 2010, 3:58:46 PM4/4/10
to
Chad <cda...@gmail.com> wrote:
[...]

> int main(void)
> {
> int x = 1;
>
> while(x) {
> foo();
> bar();
> }
>
> return 0;
> }
>
> What prevents bar() from executing before foo() is finished in this
> while loop?

The definition of the C language, which says that statements are
executed in sequence, one after another.
--
Larry Jones

They say winning isn't everything, and I've decided
to take their word for it. -- Calvin

Seebs

unread,
Apr 4, 2010, 4:51:53 PM4/4/10
to
On 2010-04-04, Chad <cda...@gmail.com> wrote:
> What prevents bar() from executing before foo() is finished in this
> while loop?

That the compiler generates code which calls foo, and then calls bar.
Calling a function doesn't complete until the function returns.
(There's some weird exceptions, involving things like "setjmp",
but you probably never need to know.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

0 new messages