473,403 Members | 2,222 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,403 software developers and data experts.

pre increment or pre decrement

void main()
{
int i=4,j=7;
printf("%d",i++ +i*j-j*i);
getch();
}


ans--> -2
why?
Oct 13 '06 #1
9 3007
vermarajeev
180 100+
void main()
{
int i=4,j=7;
printf("%d",i++ +i*j-j*i);
getch();
}


ans--> -2
why?
On my system the answer is 4
Oct 13 '06 #2
main()
{
int i=4,j=7;
printf("%d",++i+i*j-j*i);
}

output--> -2
why ?
Oct 13 '06 #3
i compiled the code and the output was 5???
what are you expecting?
Oct 13 '06 #4
82rob
7
Hi

The output is 5.

The process goes like this:
++i+i*j-j*i
++4+4*7-7*4
the process os divided now in three parts
++4 + 4*7 - 7*4
5 + 28 - 28

So answer is 5...how come it is -2 ? Check it out again please...
Oct 13 '06 #5
geek491
21
Hi

The output is 5.

The process goes like this:
++i+i*j-j*i
++4+4*7-7*4
the process os divided now in three parts
++4 + 4*7 - 7*4
5 + 28 - 28

So answer is 5...how come it is -2 ? Check it out again please...
Hi all,

The output is -3 :) well so many ppl are coming up with so many ans so execution of code is must before someone tries to ans it... :)

based on precedence ++ then * and then + -

so now the equation will be
i++ first.
Now in equation replace i++ as 4

4 +i*j-j*i

but since it is a post increment i value will be refleceted only after an operation.

so that operation is i*j = 4 * 7 =28

then the value of i is visible as 5

so the next operation is j * i = 7 * 5 = 35
here i is 5 since a single operation 4*7 occured before it.

now the whole equation becomes.

4 + 28 - 35 = -3

Hope all are happy with ans.... if some one is not satisfied then they can execute the code in a turbo c complier and verify the result.

let me know on how it helped to geek491@yahoo.co.in
thanks..bye
Oct 13 '06 #6
82rob
7
Hii Geek,

If I am not wrong you are speaking about post increment. But it looks like it is pre-increment. I think the process goes like this:-

++i+i*j-j*i
First pre-increment --->>>
++4+i*j-j*i
New value of i will be 5
5 + i*j-j*i
now,
5 + 5*7 - 7*5

So answer is again 5...If I am wrong then I would surely like to hear the correct answer from your side.
Hi all,

The output is -3 :) well so many ppl are coming up with so many ans so execution of code is must before someone tries to ans it... :)

based on precedence ++ then * and then + -

so now the equation will be
i++ first.
Now in equation replace i++ as 4

4 +i*j-j*i

but since it is a post increment i value will be refleceted only after an operation.

so that operation is i*j = 4 * 7 =28

then the value of i is visible as 5

so the next operation is j * i = 7 * 5 = 35
here i is 5 since a single operation 4*7 occured before it.

now the whole equation becomes.

4 + 28 - 35 = -3

Hope all are happy with ans.... if some one is not satisfied then they can execute the code in a turbo c complier and verify the result.

let me know on how it helped to geek491@yahoo.co.in
thanks..bye
Oct 13 '06 #7
Banfa
9,065 Expert Mod 8TB
main()
{
int i=4,j=7;
printf("%d",++i+i*j-j*i);
}

output--> -2
why ?
Actually it is undefined behaviour which is why you are all getting different results. You have tried to alter a variable and access it more than once between 2 sequence points.

The result of undefined behaviour is that all bets are off the compiler is free to do what ever it likes.
Oct 13 '06 #8
On my system the answer is 4

modifying same variable in a single expression is generally undefined
It vary from compiler to compiler
Oct 13 '06 #9
Banfa
9,065 Expert Mod 8TB
ronitonline2006 please don't double post, you have created much confusion by posting the same problem twice.
Oct 13 '06 #10

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

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
3
by: shivapadma | last post by:
i want clear explanation of increment and decrement operators in c. can any body help in solving the following expression a=10 c=++a + ++a when i am executing this code ,the answer is c=24...
3
by: suman das | last post by:
slove this problem include<stdio.h> include<conio.h> void main() { int i=1; printf("%d",i++*++i); getch(); }
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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...
0
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...
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.