Hello there. I need help with this program. -
{
-
int a=10,b;
-
b=a++ + ++a;
-
printf("%d,%d,%d,%d",b,a++,a,++a);
-
}
-
The output of this program is 22,13,13,13. But in the printf statement "a++" should print 11 instead of 13, "a" should print 10 instead of 13 and "++a" should print 11 instead of 13. That is what i can make from this program. Can anybody explain me this code?
6 1340
When you invoke the ++ operator (either pre- or post-increment) twice in the same statement, you invoke undefined behavior. How your compiler decides to resolve that is completely compiler-dependent. I'd be lucky to get the same output on my compiler.
Ok but if i reduces that program to this -
{
-
int a=10;
-
printf("%d,%d",a,a++);
-
}
-
In this program my compiler is printing 11 first and then 10. Why?
Ok but if i reduces that program to this -
{
-
int a=10;
-
printf("%d,%d",a,a++);
-
}
-
In this program my compiler is printing 11 first and then 10. Why?
There's a paragraph in the C and C++ Standard that deals with "undefined behaviour".
Being dependent on the order of evaluation of your parameters simply induces
that undefined behaviour. Some compilers evaluate actual function parameters
from left to right; other evaluate them from right to left. You can't depend on it.
kind regards,
Jos
There's a paragraph in the C and C++ Standard that deals with "undefined behaviour".
Being dependent on the order of evaluation of your parameters simply induces
that undefined behaviour. Some compilers evaluate actual function parameters
from left to right; other evaluate them from right to left. You can't depend on it.
kind regards,
Jos
what if i want "a" to be print first, then what should i do?
what if i want "a" to be print first, then what should i do?
Use two printf() calls.
When you invoke the ++ operator (either pre- or post-increment) twice in the same statement, you invoke undefined behavior. How your compiler decides to resolve that is completely compiler-dependent. I'd be lucky to get the same output on my compiler.
refer this you will get the whole idea....
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bill Davy |
last post by:
To make life easier for my users, I'd like to colour my prompt string (as
handed to raw_input()) a different colour to that produced by print. I'm
using Python 2.4.1 and IDLE 1.1.1 on Windows XP. ...
|
by: Dave |
last post by:
I'm getting different results when I display a value in
the Output window as opposed to the Immediate window.
Why?
Code to recreate problem:
'Purpose: Get different results from Output...
|
by: Raja Kiran Kumar Reddy Sandireddy |
last post by:
Hi,
I observed a very strange phenomenon with my program outputs. I run
my C-program on MSDOS environment and the same program on Sun-session.
I get same output when the input is a small file...
|
by: ypjofficial |
last post by:
Hello All,
since the programs' stack is shared among all the function inside the
program,
I was just doing some R&D to see whether the same stack space is used
for the variables inside the...
|
by: Bruce |
last post by:
I am using VB in Vs2005. Am I missing something or does VB not have the
concept of "builds" (release/debug) like in VC? I wrote an assembly
and I would like to have a debug version of the DLL...
|
by: Alfons Puig |
last post by:
Using the same xml and xsl files, the result of the xmlTransform.Transform
is different depending if a xmlTextwriter or a StringWriter is used. In the
first case, the output file is as expected,...
|
by: Hongbo |
last post by:
Hi,
I use System.Security.Cryptography.HashAlgorithm.ComputeHash() method
with SHA512 to encrypt password.
I recently upgrade my website from .Net 1.1 to .Net 2.0. The passwords stop working....
|
by: y2ktan |
last post by:
Hi Ladies and Gentlements,
I have a variable with the type of double. Let said that it is 0.003.
double dblValue = 0.003
Now, when I want to convert it to string with the local regional...
|
by: Primo |
last post by:
I have 3 files and I want all of them to go to the bank and one of them will go to both the bank and a vendor.
this is a code we have:
<process name="FD_Generic_Outbound_banksplit">
...
|
by: pereges |
last post by:
I have a C program which I created on Windows machine. I have compiled
and executed the program on windows machine and it gives me the
consistent output every time i run it. for eg.
input a = 2,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |