473,387 Members | 1,520 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,387 software developers and data experts.

Different output

Hello there. I need help with this program.
Expand|Select|Wrap|Line Numbers
  1. int a=10,b;
  2. b=a++ + ++a;
  3. printf("%d,%d,%d,%d",b,a++,a,++a);
  4. }
  5.  
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?
Nov 12 '07 #1
6 1340
Ganon11
3,652 Expert 2GB
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.
Nov 12 '07 #2
Ok but if i reduces that program to this
Expand|Select|Wrap|Line Numbers
  1. {
  2. int a=10;
  3. printf("%d,%d",a,a++);
  4. }
  5.  
In this program my compiler is printing 11 first and then 10. Why?
Nov 12 '07 #3
JosAH
11,448 Expert 8TB
Ok but if i reduces that program to this
Expand|Select|Wrap|Line Numbers
  1. {
  2. int a=10;
  3. printf("%d,%d",a,a++);
  4. }
  5.  
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
Nov 12 '07 #4
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?
Nov 12 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
what if i want "a" to be print first, then what should i do?
Use two printf() calls.
Nov 12 '07 #6
kky2k
34
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....
Nov 12 '07 #7

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

Similar topics

4
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. ...
2
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...
13
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...
10
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...
8
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...
1
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,...
6
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....
1
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...
1
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"> ...
87
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,...
0
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,...
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: 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$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
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,...

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.