Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.std.c > #6417 > unrolled thread
| Started by | Satan <satan@dot.com> |
|---|---|
| First post | 2022-01-17 03:00 +0000 |
| Last post | 2022-01-16 23:18 -0500 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.std.c
Is the output of this program compiler dependent? Satan <satan@dot.com> - 2022-01-17 03:00 +0000
Re: Is the output of this program compiler dependent? Richard Damon <Richard@Damon-Family.org> - 2022-01-16 23:18 -0500
| From | Satan <satan@dot.com> |
|---|---|
| Date | 2022-01-17 03:00 +0000 |
| Subject | Is the output of this program compiler dependent? |
| Message-ID | <ss2nci$a2k$1@gioia.aioe.org> |
I read somewhere that the parameters of functions are evaluated right >>
to left but it all depends on the compiler. Therefore, does this mean
that the output of this program is "undefined" meaning there is no
definite answer!
#include <stdio.h>
void fun(int, int);
int main()
{
int i = 5;
fun(--i, i++);
fun(++i, i--);
printf("From Main: %d\n", i++);
return 0;
}
void fun(int x, int y)
{
printf("From Function: %d %d\n", x++, y--);
}
[toc] | [next] | [standalone]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2022-01-16 23:18 -0500 |
| Message-ID | <X56FJ.6201$rQ.6013@fx18.iad> |
| In reply to | #6417 |
On 1/16/22 10:00 PM, Satan wrote:
> I read somewhere that the parameters of functions are evaluated right >>
> to left but it all depends on the compiler. Therefore, does this mean
> that the output of this program is "undefined" meaning there is no
> definite answer!
>
> #include <stdio.h>
>
> void fun(int, int);
>
> int main()
> {
> int i = 5;
> fun(--i, i++);
> fun(++i, i--);
> printf("From Main: %d\n", i++);
>
> return 0;
> }
>
> void fun(int x, int y)
> {
> printf("From Function: %d %d\n", x++, y--);
> }
Yes, there is no sequencing between the evaluation of the various
parameters to the function, to those calls invoke undefined behavior.
Implementations might give additional promises, but that is beyond the
control of the standard, and there may be a 'normal' order they follow,
but that still doesn't make it a requirement, and you might get burned
by a case where the implementation did it different in some case because
of ...reasons... so do go by what seems to work.
Remember, doing what you expected is perfectly fine possible undefined
behavior, but you shouldn't count on it.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.std.c
csiph-web