Multiple function calls inside a while loop question

C

Chad

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
 
L

lawrence.jones

Chad said:
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.
 
M

Malcolm McLean

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

Seebs

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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top