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

error output on incrementing

hai,
iam a new to C and also new to this site , please give me the explaination to the following program why the out put is 56.

int i=7;
printf("%d",i++*i++);
Nov 29 '06 #1
4 1414
horace1
1,510 Expert 1GB
hai,
iam a new to C and also new to this site , please give me the explaination to the following program why the out put is 56.

int i=7;
printf("%d",i++*i++);
with i++ the value of the operand is used in the expression then it is incremented. e.g. assuming i = 10
Expand|Select|Wrap|Line Numbers
  1.    k = i++; after assignment k would = 10 and i = 11
  2.  
Remember, in addition to evaluating the expression, the value of the variable in memory is altered (this is called a side effect). Do not use a variable more than once in an expression if one (or more) of the references has one of these operators attached to it. The C/C++ standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value.

so in your example
Expand|Select|Wrap|Line Numbers
  1. i++*i++
  2.  
when is the ++ done? you don't know. Different compilers give different results.

also see post
http://www.thescripts.com/forum/thread570241.html
Nov 29 '06 #2
smithan
16
hai,
iam a new to C and also new to this site , please give me the explaination to the following program why the out put is 56.

int i=7;
printf("%d",i++*i++);

hi,

u must have guessed that the output is 56 because it is taking the value of 7*8 ..... now when "i" has the value 7 it multiplies with 8 because, of the expression i++......

i++, "++" in this expression increaments the current value of i by "1" , but it does so only after "i" is used......

so, this is what is happening i ur program....

i=7; // the value of "i" is set to "7" .....
printf("%d",i++*i++); // i is 7 first n then it is increamented hence it becomes 8, so, 7*8 is taken as during the second occurance of "i" its value would already be incremented........ this is how the statements look after execution....

i=7
printf("%d", 7*8);
// i =9 at the end, coz u have increamented i in its last occurance.....

hence the result being 56

smitha
Nov 29 '06 #3
smithan
16
hai,
iam a new to C and also new to this site , please give me the explaination to the following program why the out put is 56.

int i=7;
printf("%d",i++*i++);

and also if ur trying to print the value of 7* 7 then u have to use
printf("%d",i*i);

and incase u want to increment the value of i after multiplying then use
printf("%d",i*i++);

smitha
Nov 29 '06 #4
horace1
1,510 Expert 1GB
and also if ur trying to print the value of 7* 7 then u have to use
printf("%d",i*i);

and incase u want to increment the value of i after multiplying then use
printf("%d",i*i++);

smitha
Not advisable! remember in addition to evaluating the expression i*i the value of the variable i in memory is altered (this is called a side effect). The standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value. For example
Expand|Select|Wrap|Line Numbers
  1.     int i=7;
  2.     printf("%d",i*i++);
  3.  
prints
49
using DEV-C++ with the GNU gcc compiler and
56
when using Turbo C V3.01

simillarly your original code
Expand|Select|Wrap|Line Numbers
  1. int i=7;
  2. printf("%d",i++*i++);
  3.  
prints
49
using DEV-C++ with the GNU gcc compiler and
56
when using Turbo C V3.01

Do not use a variable more than once in an expression if one (or more) of the references has i++ or i-- attached to it. You don't know when the side effects takes place.
Nov 29 '06 #5

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
3
by: Michael Ahlers | last post by:
Obviously if you're looping or using a template, choosing output based on the current iteration is easy. For example, if you're walking a set of elements and you want index % 2 == 0 produce one...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
2
by: Payson Books | last post by:
Where can I find some info on grabbing the recordID of the last record, incrementing it by one, and using the new number in a new record.
11
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use...
26
by: Bas Wassink | last post by:
Hi there, Does the ANSI standard say anything about incrementing variables past their limits ? When I compile code like this: unsigned char x = 255; x++; printf ( "%d\n", x );
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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?
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...

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.