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

problem to solve this expression

amarniit
sir i am submitting a C program code in which i am getting the diffrent diffrent answer for a same expression....... and i am not getting that in it how the work of pre and post increment in these expressions.
in this program befor each expression j=2
************************************************** ******************************************
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main ()
  3. {
  4.     int j=2,k;
  5.     int i=j++*++j*j;
  6.     j=2;
  7.     k=j++*++j*j;
  8.     printf("\n\nThe value of int i = %d",i);
  9.     printf("\n\nThe value of k = %d",k);
  10.     j=2;
  11.     printf("\n\nDirect value of this expression j++*++j*j = %d",j++*++j*j);
  12.     printf("\n\n***********************************************************");
  13.     j=2;
  14.     printf("\n\nDirect value of this expression j*++j = %d",j*++j);
  15.     j=2;
  16.     k=j*++j;
  17.     printf("\n\nThe value of k = %d",k);
  18.     j=2;
  19.     printf("\n\nDirect value of this expression j*j++ = %d",j*j++);
  20.     j=2;
  21.     printf("\n\nDirect value of this expression j++*j++ = %d",j++*j++);
  22.     j=2;
  23.     printf("\n\nDirect value of this expression ++j*++j = %d",++j*++j);
  24.     j=2;
  25.     printf("\n\nDirect value of this expression ++j*j++ = %d",++j*j++);
  26.     j=2;
  27.     printf("\n\nDirect value of this expression j++*j++ = %d",j++*j++);
  28.     j=2;
  29.     printf("\n\nDirect value of this expression j*j++*++j = %d",j*j++*++j);
  30.     j=2;
  31.     printf("\n\nDirect value of this expression j++*++j = %d",j++*++j);
  32.     j=2;
  33.     printf("\n\nDirect value of this expression ++j*j++*j = %d",++j*j++*j);
  34.     j=2;
  35.     printf("\n\nDirect value of this expression ++j*++j*j++ = %d",++j*++j*j++);
  36.     j=2;
  37.     printf("\n\nDirect value of this expression j++*++j*j = %d",j++*++j*j);
  38.     printf("\n\n***********************************************************");
  39. getch();
  40. clrscr();
  41. }
  42.  
************************************************** ******************************************
this is all my a small program...
Sep 17 '09 #1
6 2696
weaknessforcats
9,208 Expert Mod 8TB
int i=j++*++j*j;
This doesn't work.
In a single statement, changing a variable more than once results in indeterminant results.
Sep 17 '09 #2
Boss you told me only one statement. but my dear i have these too much statement and in it i am not getting the correct answer>>>>>>>>>
well thanks for it
Sep 18 '09 #3
Dheeraj Joshi
1,123 Expert 1GB
Hi.. In our organization we are told to avoid cascading ++i , i++ etc in an expression. Rather we follow i=i+1 statement then cascade i in the expression. This purely to avoid confusion and to avoid undesired nature.. So you strip your expression

Expand|Select|Wrap|Line Numbers
  1. int i=j++*++j*j;
  2.  
into some simple expressions, and then try to solve it..(And same can be done for all the other expressions)
Take care with ++i, and i++.

Regards
Dheeraj Joshi
Sep 18 '09 #4
Dheeraj Joshi
1,123 Expert 1GB
Hi experts..... Please correct if i am wrong...

I think execution of

Expand|Select|Wrap|Line Numbers
  1. int i=j++*++j*j;
  2.  
varies from compiler to compilers(Left to right and right to left execution).

Regards
Dheeraj Joshi
Sep 18 '09 #5
Banfa
9,065 Expert Mod 8TB
@dheerajjoshim
This is more or less right although you have your terminology a little wrong.

Multiple access to any variable between sequence points in a C++ (or C) program where one or more of those accesses is a modifying access produces undefined behaviour and should be avoided.

Every single calculation in the first post to this thread is an access of that nature, additionally since main always returns int and the program is written with main returning void that also produces undefined behaviour.

The whole output of this program is undefined behaviour because as soon as undefined behaviour is invoked in anyway that is the only result of the program.

Undefined behaviour
Sequence Points
Sep 18 '09 #6
Dheeraj Joshi
1,123 Expert 1GB
Thanks for the info BANFA....and Thanks for the LINKS...

I don't know how i missed those links when i searched about it previously :)

Regards
Dheeraj Joshi
Sep 18 '09 #7

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

Similar topics

49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
1
by: Michael B Allen | last post by:
I have a macro that can be used like: E(errno); but for reasons of brevity I frequently use the form: E(errno = EINVAL); But I would like to be able to have two definitions such that it...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
9
by: Frank | last post by:
Hi, I use datatable.select to select row in a table in a dataset. I use an ampersand in the selectstring and get an error that it is unsupported. The statement works fine in the query analyzer of...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
3
by: Senna | last post by:
Hi all, Thru a HttpWebRequest and HttpWebResponse I get html like this(<pre> excluded). <pre> <tr> <td valign="top" align="right" class="SgSVT1BG" nowrap style=" padding-right: 6px "><div...
5
by: Martin Chen | last post by:
I have a frame set (as per MS FrontPage 2000). It has a contents and a main frame. The contents frame has a menu bar written with with javascript (in the context of a table). In IE6.1 everything...
8
by: Shawn B. | last post by:
Greetings, Me again. I have (roughly) the following code: HANDLE hConsoleOutput; HANDLE hConsoleInput;
5
by: Grzegorz Danowski | last post by:
Hi, I'd like to read all lines of caption text from a string: .... Name ="Paragraph" Caption ="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. " "The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.