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

Prob in opreator?

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3. int i=2;
  4. printf("%d %d",++i,++i)
  5. getch();
  6. }
  7.  
The ans of this program is 4 3
but in my opinion the ans should be 3 3
plz explain why ans 4 3 is comming..........
May 13 '10 #1
11 1685
hype261
207 100+
The reason why the answer is 4 3 is because C evaluates arguments from right to left. So...

Expand|Select|Wrap|Line Numbers
  1. printf("%d %d, ++i; ++i) <--- second ++i is evaluated first
Also you are using the preincrement operator(++i) which means you increment i before it is evaluated.

if you did

Expand|Select|Wrap|Line Numbers
  1. printf("%d %d, i++, i++) 
your answer would be 3 2
May 13 '10 #2
donbock
2,426 Expert 2GB
C does not evaluate arguments from right to left.
In fact, the order of evaluation is explicitly labeled implementation-defined. That means C is free to use whatever order it feels like. You might get one result now, but a different one tomorrow. You should consider the output of this program to be unpredictable.
May 14 '10 #3
hype261
207 100+
Sorry for the miss information. Guess I missed that little tid bit of knowledge in portability.
May 14 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
It's nothing to do with portability. Instead, the rule is that since the order of evaluation is undefined then referring to an variable twice can produce indeterminant results of one or more of the references change the value of the variable.

This is OK:

Expand|Select|Wrap|Line Numbers
  1. if (++i)
  2. etc...
but this is not:

Expand|Select|Wrap|Line Numbers
  1. if (++i  == --i)
  2. etc...
May 14 '10 #5
hype261
207 100+
@weaknessforcats
So are you saying that the compilier itself will not evaluate the function the same way everytime?
May 14 '10 #6
donbock
2,426 Expert 2GB
@hype261
The compiler is not required to evaluate the operators the same way every time, even if you don't change the expression. Apparently inconsequential changes to the expression or the command line switches might cause the optimizer to do the preincrements in a different order.
May 14 '10 #7
hype261
207 100+
@donbock
Good to know, thank you.
May 14 '10 #8
@hype261
But in a book the ans of this expression is given 4 3 not give the ans that "it will varry from one compiller to othetr compiller....."

other thing may i know is that if i write
i=2;
i=++i
then the value of i changed or produce an error in a program
May 14 '10 #9
donbock
2,426 Expert 2GB
@AnagJohari
Expand|Select|Wrap|Line Numbers
  1. printf( "%d %d", ++i, ++i);
What book? Are you certain the example in the book was precisely this?



Expand|Select|Wrap|Line Numbers
  1. i = 2;
  2. i = ++i;
Nothing mysterious here. The value of i should end up as 3. Are you saying the compiler gave you an error for these instructions?
May 14 '10 #10
@donbock
i m actually finding the concept of this finding the output of an programe
there are four options ,,, one of the option is 4 3
& second one is ans vary from one compiller to other compiller..
in a book ans is given 4 3
but u suggest the ans vary from one compiller to other
so i m in coonfused....
May 15 '10 #11
weaknessforcats
9,208 Expert Mod 8TB
All you need to remember is that changing a variable more than once in the same statement produces indeterminate results.

Indeterminate means you can't predict the result. Maybe it works, maybe it doesn't.

BAD:

Expand|Select|Wrap|Line Numbers
  1. printf("%d%d\n", ++i, --i);
GOOD:

Expand|Select|Wrap|Line Numbers
  1. printf("%d", ++i);
  2. printf("%d\n", --i);
May 15 '10 #12

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

Similar topics

6
by: josephrthomas | last post by:
hi..i am trying to make a login page and i am using access table.. when the user enters his userid and password i want to check the password from the table.. if any user with the userID that is...
29
by: Thomas | last post by:
Hi I have an XSL stylesheet: <xsl:for-each select="TRACKS/TRACK"> <tr class="TDL"> <td width="90%"><xsl:number value="position()" format="1" /> - <xsl:value-of select="TRACKTITLE"/></td>...
5
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h>...
2
by: Dishan Fernando | last post by:
Hi my prob is like this.. ----------------------------- create table ax( i int , j int ) create table ay( i int ,
15
by: Raj | last post by:
Hello all: We have a table with about 2400 cells. Our requirement is to highlight the cells in the table whose data has changed, every 5 seconds. Our script behaves relatively ok in Firefox, but...
6
by: skubik | last post by:
Hi everyone. I'm attempting to write a Javascript that will create a form within a brand-new document in a specific frame of a frameset. The problem is that I can create the form and input...
2
by: Wilhelm Kutting | last post by:
hi, i like to solve a code-prob: tidy is bitching about <...> proprietary attribute "type" <...> lacks "action" attribute how can i make this nice? this is my code
28
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use...
0
Savage
by: Savage | last post by:
I'm making for fun a simple program which format a input file.Input file sustain of person name,lastname and date of birth.Output file si supposed to be forammted as following: NAME ...
2
by: mnacw | last post by:
Can anybody help me to resolve this prob. i have installed Visual Studio 2005 Professional edition. I am working in VB.Net. When I tried to connect to database it is connected but when i make some...
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
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
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
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,...
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...
0
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,...

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.