473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

function not following usual Parameter Passing Sequence

Consider following code :

I have used code blocks for compiling.

output:
121 12
144 10

shouldn't the first line of output be 121 10 (considering the R-L passing of parameter)

Expand|Select|Wrap|Line Numbers
  1. int main(){
  2. int i=10,j=10;
  3. printf("%d%d\n",++i*i++,i);
  4. printf("%d%d",++j*j++,j++);
  5.  
  6. }
  7.  
Oct 14 '10 #1
3 1218
Meetee
931 Expert Mod 512MB
This is the magic of C/C++.

i = 10
++i = 11
++i*i++ = 121 and now i becomes 12 -> consider it as (++i*i) and then ++
Here compiler doesn't ++ for the second term for multiplication (and does ++ after multiplication)
i = 12

So the output = 121 12
Oct 14 '10 #2
newb16
687 512MB
There is no such thing as usual parameter passing sequence, it's not even implementation-defined and compiler is free to choose any order in any time.
Oct 14 '10 #3
Oralloy
988 Expert 512MB
What you have is an example of undefined behaviour.

The C/C++ specifications state that the compiler may evaluate an expression in any order.

Thus any expression which has multiple elements with side-effects makes no guarantee as to the result. Specifically, if there are multiple side effects on the same memory space, there is no guarantee of their application order.

Your expression in line 3 has side effects on the variable i twice, and uses the value of i three times. With no guarantee on execution order, what do you expect the result to be?

Also, as a side note, many compilers compute the arguments to functions/subroutines/methods in reverse order, so that they are pushed on the stack in correct order, and thus in ascending memory positions in the argument list. This, of course, assumes a stack which grows downward in memory.

Hopefully that helps.

Luck.
Oct 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
3
by: Andrew | last post by:
Hello, Is it bad practice to use a function as a parameter of a macro? Will the compiler create a full copy of the function's machine code for each invocation of the macro? Or does it create...
2
by: xideum | last post by:
Hi. Question: In a function that takes multiple arguments, each with a default value setis there anyway to either alter the order of the parameters you pass to it or skip the passing of certain...
0
by: KK | last post by:
Hi MCPP gurus' I have a small problem in parameter passing.This is my situation namespace Abc { public __gc class ClassAbc { public: String* Hello(String* str) {
0
by: Richard Buckshaw | last post by:
Mimick the older C/ pascal dll parameter passing convention? - VB Class Mod? Hello, I have been attempting to write a VB class that would expose its stuff to an older (ok, legacy application)...
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
1
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter...
16
by: Theo R. | last post by:
Hi all, Does the C99 Standard explicitly mention the need for a stack for passing arguments or Is this platform specific? As an example, the ARM9 processor recommends Core Registers R0-R3 be...
13
by: frakie | last post by:
Hi 'body, I'm experiencing difficulties on parameter passing. I wrote a library in last months, and now it crashes few times in a month because of errors in parameter passing: using gdb on the...
3
by: joe | last post by:
Consider the following program: include <iostream> class Bar { public: int getData9() { m_data = 9; return m_data;} int getData11() { m_data = 11; return m_data;} int m_data;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.