Results 1 to 16 of 16

Thread: How is it calculated???

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    How is it calculated???

    If we have
    a=5;
    c=++a + ++a + ++a;

    What would be value of c after this statement and how does it come?

    If we have the code
    a=5;
    c=a++ + a++ + a++;

    What would be the value of c then?
    And also please explain how it is calculated by the compiler???


  2. #2
    Contributing Member
    Join Date
    Oct 2007
    Answers
    88

    Smile Re: How is it calculated???

    Quote Originally Posted by ashwini_nith View Post
    If we have
    a=5;
    c=++a + ++a + ++a;

    What would be value of c after this statement and how does it come?

    If we have the code
    a=5;
    c=a++ + a++ + a++;

    What would be the value of c then?
    And also please explain how it is calculated by the compiler???
    left to right assigment
    c= ++a + ++a + ++a;
    <- <- <-
    (5) + (5) +(5);
    c=15;


  3. #3
    Junior Member
    Join Date
    Nov 2007
    Answers
    9

    Re: How is it calculated???

    Hi!

    In this equatin c=++a+ ++a+ ++a;
    The "a " value is changing every step so, ++a is consider as a=1+a;
    In this case, "a" value is changing it's memory location(&a).
    so final value of "a" is a=8. you should put the "a" value as 8 in this equation.
    c=a+a+a; c=8+8+8;c=24;

    you should not do this problem as like this c=++a+ ++a+ ++a;
    c=6+7+8 --->false



    Following procedure may help you

    int a=5;
    c=++a+ ++a+ ++a; ---step(1)

    (((((((((((This case output is 24

    c=(++a)+( )+( );
    c=(++5)+( )+( ); ---Step(2)

    After this step a=6; &a-->6

    c=a+ ++a+( );
    c=a+ ++6+( ); ---(3)

    After This step a=7; &a-->7

    c=a+a+ ++a;
    c= a+a+ ++7; ---(4)

    After This step a=8;&a-->8

    c=a+a+a;
    c=8+8+8;

    Finally c=24;


    In the next case, You will follow above procedure. It will work well.

    Even after any confusion ask me that i will help you


  4. #4
    Junior Member
    Join Date
    Nov 2007
    Answers
    9

    Re: How is it calculated???

    Hi!

    Your first case output is 24
    c=++a+ ++a+ ++a;--->(1)

    In every step "a" value is change it's memory location.

    Finally a=8; i.e &a=8;

    so, c=(++a)+ (++a)+ (++a);
    c= 8+8+8;c=24;

    This step may help you

    c=(++a)+ (++a)+ (++a);---->(2)

    c=(++5)+()+();

    c=(&a=6)+()+();--->(3)

    c=()+(++6)+();

    c=()+(&a=7)+();--->(4)

    c=()+()+(++7);

    c=()+()+(&a=8);--->(5)

    Now &a=8

    Finally, c=8+8+8;

    c=24;

    You will follow this steps to your second case.

    Every steps "a " variable value is changing in it's memory location.


    If any dought you will ask me


    Regards
    venkat.G


  5. #5
    Junior Member
    Join Date
    Nov 2007
    Answers
    9

    Re: How is it calculated???

    Hi!

    your second case output is 15
    c=a++ +a++ +a++;

    The "+" "=" operators are first precedence then post increment operator "++"
    ********************************************************************************************
    NOTE:
    (But, not in preincrement eg:++i.Hence, preincrement operator is first precedence then "+" and "=" ).
    *******************************************************************************************

    so, first addition(+) will be done and after it's value assign(=) in variable "c".

    i.e c=a++ +a++ +a++;
    c=5++ +5++ +5++;
    c=15 and a=5-----step(1)
    variable "a" is increment 3 times after step(1)
    so, a=a+3
    a=8;


    Finall output is c=15 and a=8



    EXAMPLES;

    int b=5,d;
    d=b++;
    what is the value of d?

    you know d=5 as well as b is incremented by 1. so, b=6;

    Ques1: int d,b=5;
    d=b++; ------>(a)
    Ans: d=b; ------>Step1
    b=b+1; ------>step2
    d=5; and b=6;

    Ques2: int t=3,s;
    s=t++ +t++;
    Ans:
    s=t+t; ----->step(1)
    t=t+2;

    s=6; and t=5;

    Ques3:
    int l=5,m;
    m=l++ +l++ +l++;
    Ans:
    m=l+l+l; ---->step1
    l=l+3; ------>step2
    m=15; and l=8;

    Reply your suggestion .

    Regards
    venkat.G


  6. #6
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: How is it calculated???

    Quote Originally Posted by ashwini_nith View Post
    If we have
    a=5;
    c=++a + ++a + ++a;

    What would be value of c after this statement and how does it come?

    If we have the code
    a=5;
    c=a++ + a++ + a++;

    What would be the value of c then?
    And also please explain how it is calculated by the compiler???
    Dear ashwini_nith,

    In the first case, value of 'a' will be incremented first and then it will be used
    resulting in the following:

    O/P in first case will be 6+7+8=21

    In the second case, value of 'a' will be used first and then incremented
    resulting in the following:

    O/P in second case will be 5+6+7=18

    I hope this will help you.

    Have a pleasant time.


  7. #7
    Contributing Member
    Join Date
    Oct 2007
    Answers
    88

    Re: How is it calculated???

    Quote Originally Posted by venki_2007 View Post
    Hi!

    Your first case output is 24
    c=++a+ ++a+ ++a;--->(1)

    In every step "a" value is change it's memory location.

    Finally a=8; i.e &a=8;

    so, c=(++a)+ (++a)+ (++a);
    c= 8+8+8;c=24;

    This step may help you

    c=(++a)+ (++a)+ (++a);---->(2)

    c=(++5)+()+();

    c=(&a=6)+()+();--->(3)

    c=()+(++6)+();

    c=()+(&a=7)+();--->(4)

    c=()+()+(++7);

    c=()+()+(&a=8);--->(5)

    Now &a=8

    Finally, c=8+8+8;

    c=24;

    You will follow this steps to your second case.

    Every steps "a " variable value is changing in it's memory location.


    If any dought you will ask me


    Regards
    venkat.G
    you are wrong c=15 is the answer


  8. #8
    Contributing Member
    Join Date
    Nov 2007
    Answers
    53

    Re: How is it calculated???

    Quote Originally Posted by ashwini_nith View Post
    If we have
    a=5;
    c=++a + ++a + ++a;

    What would be value of c after this statement and how does it come?

    If we have the code
    a=5;
    c=a++ + a++ + a++;

    What would be the value of c then?
    And also please explain how it is calculated by the compiler???
    Dear ashwini_nith,

    In the first case, value of 'a' will be incremented first and then it will be used
    resulting in the following:

    O/P in first case will be 6+7+8=21

    In the second case, value of 'a' will be used first and then incremented
    resulting in the following:

    O/P in second case will be 5+6+7=18

    I hope this will help you.

    Have a pleasant time.


  9. #9
    Junior Member
    Join Date
    Nov 2007
    Answers
    9

    Re: How is it calculated???

    Quote Originally Posted by sarathi trichy View Post
    you are wrong c=15 is the answer

    Hi!

    you will check the program at TURBO C/C++ compiler
    To first case
    c=++a+ ++a+ ++a;
    if a=5
    c=24;
    To second case

    c=a++ +a++ +a++;
    if a=5;
    c=15;
    -----------------> you will check in LINUX2.6.X ( In my version give this output)

    To first case
    c=22;
    To second case
    c=15;

    BUT, TURBO C/C++ compiler only correct.




    Regards
    venkat.G


  10. #10
    Contributing Member
    Join Date
    Oct 2007
    Answers
    88

    Re: How is it calculated???

    Quote Originally Posted by sreekumar_nair_it View Post
    Dear ashwini_nith,

    In the first case, value of 'a' will be incremented first and then it will be used
    resulting in the following:

    O/P in first case will be 6+7+8=21

    In the second case, value of 'a' will be used first and then incremented
    resulting in the following:

    O/P in second case will be 5+6+7=18

    I hope this will help you.

    Have a pleasant time.
    you are wrong tne answer is 15 only


  11. #11
    Junior Member
    Join Date
    Nov 2007
    Answers
    1

    Re: How is it calculated???

    Quote Originally Posted by ashwini_nith View Post
    If we have
    a=5;
    c=++a + ++a + ++a;

    What would be value of c after this statement and how does it come?

    If we have the code
    a=5;
    c=a++ + a++ + a++;

    What would be the value of c then?
    And also please explain how it is calculated by the compiler???
    hi aswani, does that syntax exists ? i think increment operator lone can exist to the right of assignment operator. check the syntax.

    Last edited by nagarjun_p002; 12-03-2007 at 10:34 PM. Reason: syntax may be right

  12. #12
    Junior Member
    Join Date
    Jun 2008
    Answers
    2

    Re: How is it calculated???

    Hi all,

    The answer of Venki is ONLY correct answer. As well as the explanation given by Venki is absolutely correct as per C++ language is concern. Inded i validated the result on VS2005.

    Others please do give it a thought before posting a message it may misguide freshers who are new to such a beautiful language. We are here keep our loving c++ at the top and guide others to right path and solution inplace of misguiding them.

    Guys sarathi trichy , sreekumar_nair_it and nagarjun_p002 please do understand the issue and work on it by validating it on a tool before givign your opinion.

    no offence to anyone, m just another C++ fan joined this community recently.


  13. #13
    Junior Member
    Join Date
    Jan 2008
    Answers
    3

    Post Re: How is it calculated???

    ++a + ++a + ++a = undefined
    ++a + ++a + ++a + ++a + ++a = undefined

    Because C expressions can contain side effects, issues of sequencing are important in expression evaluation. Most operators impose no sequencing requirements, but a few operators impose sequence points upon their evaluation: comma, logical-AND, logical-OR and conditional.
    Thus in the expressions ++a + ++a + ++a and similar expressions are said to have side effects or undefined behavior.
    Therefore we see different values being output by different compilers as there is no rule specifying the evaluation of such expressions. These compilers assume the sequence points to be at different places.


  14. #14
    Junior Member
    Join Date
    Jan 2008
    Answers
    3

    Re: How is it calculated???

    Undefined Behavior/Side Effects

    The standards define "Between two sequence points, an object is modified more than once, or is modified and the prior value is accessed other than to determine the value to be stored"

    Different compilers will evaluate the expression differently and give different output.
    e.g int a[5]={1,2,3,4,5};
    printf("\\n%d %d",a[3],++3[a]);

    would print the value 5 5 since the sequence point in the expression is ","

    The following are the sequence points described:

    The call to a function, after the arguments have been evaluated
    The end of the first operand of the following operators:
    • logical AND &&
    • logical OR ||
    • conditional ?
    • comma ,

    The end of a full declarator: declarators
    The end of a full expression: an initializerthe expression in an expression statement
    The controlling expression of a selection statement (if or switch)
    The controlling expression of a while or do statement
    Each of the three expressions of a for statement
    The expression in a return statement
    Immediately before a library function returns
    After the actions associated with each formatted input/output function conversion
    Immediately before and immediately after each call to a comparison function, Between any call to a comparison function and any movement of the objects
    passed as arguments to that call

    refer the wiki source Sequence point - Wikipedia, the free encyclopedia

    If asked in an interview tell the output with the compiler you used and explain about the side effects in brief.


  15. #15
    Junior Member
    Join Date
    Jul 2006
    Answers
    7

    Re: How is it calculated???

    A = ++c + ++c + ++c; the ans is ......21..bcoz ++a means 1st increment and then assign the value a=++a + ++a + 6(++a) (right to left value readed by compiler[<-----]) a=++a + 7(++a) + 6 a=8 (++a) + 7 +6
    a=21.... Thats all.


  16. #16
    Junior Member
    Join Date
    Jul 2006
    Answers
    7

    Re: How is it calculated???

    A = ++c + ++c + ++c; the ans is ......21..bcoz ++a means 1st increment and then assign the value a=++a + ++a + 6(++a) (right to left value readed by compiler[<-----]) a=++a + 7(++a) + 6 a=8 (++a) + 7 +6 a=21.... Thats all. Bst wses, sankar


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact