473,671 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

evaluation of arguments to a function

Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined. But, is there something to be noted from the
output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

Output:
Testorder of printing174,
greenhorn

Nov 14 '05 #1
7 1396
"Greenhorn" <te************ @yahoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
From man pages in unix it seem the behavior in below usage of
printf() is undefined. But, is there something to be noted from the
output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;


Yes, you may deduce the behaviour of your implementation in these
circumstances. The behaviour may be different for other implementations
and/or in other circumstances.

Alex
Nov 14 '05 #2
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Greenhorn <te************ @yahoo.com> wrote:
: From man pages in unix it seem the behavior in below usage of
:printf() is undefined.

That depends on what you mean by 'undefined'. The order of processing
parameters for a function call is not mandated by the standard, but
it is mandated that the parameters be processed "as if by
assignment" before the routine itself is invoked.

The behaviour is thus just the usual behaviour in the presence of
side-effects: they can occur in any order, and in some instances
may occur varying numbers of times.

:But, is there something to be noted from the
:output of the below line.

: printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

:Output:
:Testorder of printing174,

Yes, one can learn that something is broken:
- The printf format has a space between the two %d's, but the output
shown does not
- There is no comma in the output formats, but there is one in
the output shown; it should not have occured
Other than that, one could tentative figure that the in the absence
of optimization reasons otherwise, parameters are probably processed
from right to left. There is correlation with right to left processing
and implimentations that push each parameter value onto a stack
before making the call -- as contrasted with implimentations that
allocate an amount of storage large enough to hold all the parameters
(their types are all known, so the total storage can be computed)
and then store the parameters at offsets from the beginning of the
block (which tend to use left-to-right calling so as to write the
parameter values into increasing storage with incrementing stack pointer.)
Push-onto-the stack implimentations are also correlated to
ABIs in which the stack grows upwards rather than downwards.

But all of these are tentative and usually of interest only when doing
implimentation-dependant (non-portable) interfacing with non-C routines.
Though one might want to know the direction the stack grows in order
to know whether the overall program memory layout is suitable...
which would depend upon the ABI.
--
Positrons can be described as electrons traveling backwards in time.
Certainly many Usenet arguments about the past become clearer when they
are re-interpreted as uncertainty about the future.
-- Walter Roberson
Nov 14 '05 #3
Greenhorn wrote:
Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined.
The man page is, in this instance, entirely correct as far as the C
standard is concerned. So don't do it.
But, is there something to be noted from the
output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

Output:
Testorder of printing174,


Well, if you ran it on a Quarckle Froozit using it's native compiler
that information would tell you that it was exactly 4:52:13 on the 1st
July 79 BC, since that is the only time when it ever produces that
result (setting the clock on the machine is not sufficient, it actually
has to be that date/time in reality). Of course, the Quarckle Froozit
did not exist then, so this is entirely academic.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4
Flash Gordon wrote:
Greenhorn wrote:
Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined.


The man page is, in this instance, entirely correct as far as the C
standard is concerned. So don't do it.
> But, is there something to be noted from the
output of the below line.

printf("%d %d", i = printf("order of printing"),j =

printf("Test")) ;

Actually, the behaviour is unspecified.

Assuming that printf succeeds, stdout must have received either:
Testorder of printing17 4
or
order of printingTest17 4

Nov 14 '05 #5
Flash Gordon wrote:

Greenhorn wrote:
Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined.


The man page is, in this instance, entirely correct as far as the C
standard is concerned. So don't do it.
> But, is there something to be noted from the
output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

Output:
Testorder of printing174,


Well, if you ran it on a Quarckle Froozit using it's native compiler
that information would tell you that it was exactly 4:52:13 on the 1st
July 79 BC, since that is the only time when it ever produces that
result (setting the clock on the machine is not sufficient,
it actually
has to be that date/time in reality). Of course, the Quarckle Froozit
did not exist then, so this is entirely academic.


I'm not seeing it as undefined.
The order in which function arguments are evaluated is unspecified.

--
pete
Nov 14 '05 #6
pete wrote:
Flash Gordon wrote:
Greenhorn wrote:
Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined.


The man page is, in this instance, entirely correct as far as the C
standard is concerned. So don't do it.
> But, is there something to be noted from the

output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

Output:
Testorder of printing174,


Well, if you ran it on a Quarckle Froozit using it's native compiler
that information would tell you that it was exactly 4:52:13 on the 1st
July 79 BC, since that is the only time when it ever produces that
result (setting the clock on the machine is not sufficient,
it actually
has to be that date/time in reality). Of course, the Quarckle Froozit
did not exist then, so this is entirely academic.


I'm not seeing it as undefined.
The order in which function arguments are evaluated is unspecified.


OK, I was wrong about undefined. However, Is it mandated that it always
evaluates them in the same order? Also, is it mandated that it does not
evaluate them in parallel (something that could, in theory, be done on
dual processor PCs)?

If the order does not always have to be the same what I said could still
be true ;-)

I still stand by don't do it.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7
Flash Gordon wrote:

pete wrote:
Flash Gordon wrote:
Greenhorn wrote:

Hi,
From man pages in unix it seem the behavior in below usage of
printf() is undefined.

The man page is, in this instance, entirely correct as far as the C
standard is concerned. So don't do it.

> But, is there something to be noted from the

output of the below line.

printf("%d %d", i = printf("order of printing"),j = printf("Test")) ;

Output:
Testorder of printing174,

Well, if you ran it on a Quarckle Froozit using it's native compiler
that information would tell you that it was exactly 4:52:13 on the 1st
July 79 BC, since that is the only time when it ever produces that
result (setting the clock on the machine is not sufficient,
it actually
has to be that date/time in reality). Of course, the Quarckle Froozit
did not exist then, so this is entirely academic.
I'm not seeing it as undefined.
The order in which function arguments are evaluated is unspecified.


OK, I was wrong about undefined. However,
Is it mandated that it always
evaluates them in the same order?


No.
Also, is it mandated that it does not
evaluate them in parallel
(something that could, in theory, be done on
dual processor PCs)?
The arguments have function calls.
Function calls are sequence points
and can only take place one at a time.
The subsequent assignments to i and j, can take place in any order.
If the order does not always have to be the same
what I said could still be true ;-)


No.
There's a limit to the choices that the computer can make.
Undefined behavior is different.

--
pete
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
2384
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that a key occurs with is stored in the list for that key. This code's fine, and seems pretty simple, but thanks to generator
8
2793
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def _build_used(): .... y = x + 1 .... return x, y ....
9
2925
by: Albert Wagner | last post by:
What is the evaluation context of the setTimeout args below? I have a separate Timer instance for each sprite in my program. As coded, "this.Clock" doesn't work. Thanks ahead for any advice. Albert /*==================================================== Timer.js ====================================================*/
16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
2
2050
by: Jan Engelhardt | last post by:
Hi, I was told that order of evaluation is unspecified for functions, i.e. int f = 0; print_results(modify(&f), modify(&f), modify(&f)); where i.e. modify() increases f by one. In my case w/gcc, it was evaluated from right-to-left (gcc does a nice stack optimization). Not what I expected though.
15
2935
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before bar(y), where foo() and bar() can be either functions or macros? I got into problems with this when writing some hardware-related stuff where foo() and bar()
21
4108
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
10
353
by: int main(void) | last post by:
Hi all, In the following program, #include<stdio.h> int main(void) { int x = 10; int y = 10;
7
2493
by: Peter | last post by:
I know the order of construction of member and base class objects. My question is the following: Is the order of evaluation of argument lists for these constructors also defined? E.g. can I assume that the following code is exceptions safe? Assuming that the constructor of A, B or C may throw? Can I assume that B is created after the constructor of m_sA has been called?
54
3917
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.