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

increment and decrement problem in c

slove this problem

include<stdio.h>
include<conio.h>
void main()
{
int i=1;
printf("%d",i++*++i);
getch();
}
if the ans is 3 explain it how;
there is a problem
it is found that the order of precedence of increment and decrement operator
with respect to other arithmetic operator is not well define.
in some computer the result may vary , it may be 3 and 4 both if we use torbo c++ for compilation. same for printing
printf("%d",i++*++i*++i)
result is expected 18 by RL rule by in my torbo c it gives 12 ans
plz write if u have any rule to sort it out.
Aug 8 '07 #1
3 2875
ilikepython
844 Expert 512MB
slove this problem

include<stdio.h>
include<conio.h>
void main()
{
int i=1;
printf("%d",i++*++i);
getch();
}
if the ans is 3 explain it how;
there is a problem
it is found that the order of precedence of increment and decrement operator
with respect to other arithmetic operator is not well define.
in some computer the result may vary , it may be 3 and 4 both if we use torbo c++ for compilation. same for printing
printf("%d",i++*++i*++i)
result is expected 18 by RL rule by in my torbo c it gives 12 ans
plz write if u have any rule to sort it out.
The behaivior is undefined whenever you increment a variable more than once. The result can be anything and shouldn't be relied on.
Aug 8 '07 #2
++ operator has higher priority than *...

so in case pintf("%d",i++ * ++i);
the value of i is incremented by 1 in i++ but it is not assigned in i

in case of ++i value of i is incremented by i and it is assigned in i

the initial value of i is 1

try to dry run (i++ * ++i)....in i++ the value of i is incremented...as the value is not assigned the result of i++ operation is 1

but the value which in i becomes 2...after ++i operation the value of i becomes 3

multiply 1 and 3 the result is 3

apply the same logic in the later problem...hope u will get the correct result
Oct 3 '10 #3
donbock
2,426 Expert 2GB
Look here and here and here and here.
Oct 4 '10 #4

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...
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: 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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.