13 Replies - 2962 Views - Last Post: 06 October 2012 - 07:25 AM Rate Topic: -----

#1 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 12:17 AM

Why isn't the output of this program is :-

1 2 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=1;
    printf("%i %i %i",a ,++a ,a++);
    return 0;
}



But the output is :-
3 3 1

Please clear my doubts.
Is This A Good Question/Topic? 0
  • +

Replies To: Please clear my doubts about increment/decrement operators,.

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 12:27 AM

View Postkarank6195, on 01 October 2012 - 12:47 PM, said:

Why isn't the output of this program is :-

1 2 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=1;
    printf("%i %i %i",a ,++a ,a++);
    return 0;
}



But the output is :-
3 3 1

Please clear my doubts.


I seriously don't understand how you got 3 3 1. According to the logic of increment/decrement operators, the answer should be 1 2 3.

regards,
Raghav
Was This Post Helpful? -1
  • +
  • -

#3 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 12:30 AM

No,I'm getting the output as 3 3 1
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 02:51 AM

First of all the order in which function arguments are evaluated is unspecified, so you can't rely on side-effects executing in a certain order.

Second of all modifying the same variable twice without a sequence point in between is undefined behavior.

PS: Note that none of this is specific to Objective C. It's the same in plain C.

View Postraghav.naganathan, on 01 October 2012 - 09:27 AM, said:

According to the logic of increment/decrement operators, the answer should be 1 2 3.


Even if evaluation order of function arguments were defined to be left-to-right and there were an implicit sequence point between arguments, the result would not be 1 2 3. It would be 1 2 2, i.e. the result that the OP expected.

This post has been edited by sepp2k: 01 October 2012 - 02:48 AM

Was This Post Helpful? 1
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 02:55 AM

View Postsepp2k, on 01 October 2012 - 03:21 PM, said:

First of all the order in which function arguments are evaluated is unspecified, so you can't rely on side-effects executing in a certain order.

Second of all modifying the same variable twice without a sequence point in between is undefined behavior.

PS: Note that none of this is specific to Objective C. It's the same in plain C.

View Postraghav.naganathan, on 01 October 2012 - 09:27 AM, said:

According to the logic of increment/decrement operators, the answer should be 1 2 3.


Even if evaluation order of function arguments were defined to be left-to-right and there were an implicit sequence point between arguments, the result would not be 1 2 3. It would be 1 2 2, i.e. the result that the OP expected.


Oh...I am really sorry sepp2k, I intended to type 1 2 2 itself. What I meant to ask was why does 3 3 1 come as the output for the OP ?

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 02:59 AM

If the arguments are evaluated right-to-left, 3 3 1 is actually a pretty unsurprising result. But as I said, it's undefined behavior, so the result might be anything - no matter what the evaluation order is.
Was This Post Helpful? 4
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 03:05 AM

This is standard C code, not Objective-C. Objective-C is used to develop Apple software.

This code is stupid, like sepp2k says, demonstrates undefined behavior.

See this excellent post from Salem_c which shows why it's a stupid question.

Moved to C and C++.
Was This Post Helpful? 2
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Please clear my doubts about increment/decrement operators,.

Posted 01 October 2012 - 03:51 AM

raghav.naganathan, don't PM people about the rep the give.

I down-repped you because your reply continues the illusion that there IS a right answer to this question. There is NO CORRECT ANSWER TO THIS QUESTION; if you are TOLD there is -- that is, you are being instructed that this code is in any way proper code -- you are being lied to and taught by fools.

This type of question is frequently seen from Indian students. You say you're from India, and I'll assume you've been through school. Please answer this question for me: are they teaching you over there that this code is valid code, or are they using this code to demonstrate how NOT to write code? I'm genuinely interested, and knowing the answer to this will help US in knowing how to answer this question in the future.
Was This Post Helpful? 4
  • +
  • -

#9 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 03 October 2012 - 12:33 AM

Yes! I'm an Indian student and I've been through school.I know my teacher is not a good programmer,she only has the degree of software engineering.
But I don't really rely on her.I mostly learn through internet and she taught me that the output of this program should be:-
3 2 2
because according to her the program executes from right to left.
But when I ran this program manually,I got the output as:-
3 3 1
That's why I came here at dreamcode.net
Was This Post Helpful? 0
  • +
  • -

#10 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Please clear my doubts about increment/decrement operators,.

Posted 03 October 2012 - 01:07 AM

*
POPULAR

> That's why I came here at dreamcode.net
Perhaps you could direct your tutor to this thread as well.
Was This Post Helpful? 5
  • +
  • -

#11 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Please clear my doubts about increment/decrement operators,.

Posted 03 October 2012 - 07:43 AM

You are in the world of side effects... :P

You really have to read this article: Increment/decrement operators, and side effects. It will help you a lot...

Hope this Helps!

This post has been edited by AKMafia001: 03 October 2012 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,126
  • Joined: 05-May 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 03 October 2012 - 08:14 AM

Yes, with the C calling convention, parameters are put in the stack right to left, but there is no requirement that the compiler evaluate the parameters from right to left.

On Windows platforms, functions can also be marked as using the Pascal calling convention where parameters are put on the stack from left to right. Again there is no compiler requirement that the parameters be evaluated in that order.

http://en.wikipedia....ing_conventions
Was This Post Helpful? 1
  • +
  • -

#13 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 04 October 2012 - 06:27 AM

Oh ya!!
I've got it..
Thank you so much ..
Was This Post Helpful? 0
  • +
  • -

#14 WiFi COMPUTER SYSTEM  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Please clear my doubts about increment/decrement operators,.

Posted 06 October 2012 - 07:25 AM

Quote

That Is A C++ Increment & Decrement Code Try !T

#include<iostream.h>
#include<conio.h>
class Counter
{
private:
int cv;
public:
Counter()				 // Zero Argument Constructor
{
cv=5;
cout<<"Counter Initial Value "<<cv<<endl;
}
void operator ++()                    // Overloading of ++ operator for-
{                                     // -Prefix Notation.
++cv;
}
void operator ++(int)                 // Postfix Notation.
{
cv++;
}
void operator --()                   // Overloading of -- operator for-
{                                    // -Prefix Notation.
--cv;
}
void operator --(int)                // Postfix Notation.
{
cv--;
}
void showCounter()                   // For Display.
{
cout<<"Current value "<<cv<<endl;
}};
void main()
{
clrscr();
Counter counter;
++counter;
counter.showCounter();
counter++;
counter.showCounter();
--counter;
counter.showCounter();
counter--;
counter.showCounter();
getch();
}

Was This Post Helpful? -2
  • +
  • -

Page 1 of 1