• C

increment/decrement operator usage

1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler?
void main()
          int val=5;
        printf("%d %d %d %d",val,--val,++val,val--);
under turbo compiler its giving
4 4 5 5
and under visual c++ its
 5 5 6 5

2) How to evaluate following statement
int val =5;
 val =- --val - val-- - --val;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

printf("%d %d %d %d",val,--val,++val,val--);
 val =- --val - val-- - --val;
violate the requirement that
Between the previous and next sequence point, an object shal have it's stored value modified at most once by the evaluation of an expression.
This means that the behaviour is undefined.
>>>     printf("%d %d %d %d",val,--val,++val,val--);

read this --  The Top 10 Ways to get screwed by the "C" programming language

 from http://www.andromeda.com/people/ddyer/topten.html

K&R and ANSI/ISO C specifications do not define the order of evaluation for function arguments. It can be left-to-right, right-to-left or anything else and is "unspecified". Thus any code which relies on this order of evaluation is doomed to be non portable, even across compilers on the same platform.

order of evaluation for function arguments is  "unspecified",
but modifying an object more than once between sequence points is worse than "unspecified", it is "undefined"
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

quite "unnerving" I must say ;)
>>  printf("%d %d %d %d",val,--val,++val,val--);

>> val =- --val - val-- - --val;

You don't.  In C

An item may appear as lvalue and modified, only ONCE per sequence point.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hello Mr. friend

there are basically 2 type of function clling conversions, both have their own advantanges.

1) C Type calling conversion

Hello Mr. friend

there are basically 2 type of function clling conversions, both have their own advantanges.

1) C Type calling conversion

    It pushes function parameters from right to left.
    The Advange of this method is we can N numbers parameters. (refer (...) elips operator in C manual)

2) PASCAL Type calling conversion

   It pushes function parameters from left to right
   The Advange of this method is fastet in excution.

Turboc supports only C type calling conversion.

where as in Visual studio c++ you can mension calling conversion for function.
   (refer _stdcall, PASCAL keywords in Visual Studio C++)
re: veeru_friend

It does not matter that you can specify the calling convention,
the order of evaluation for the parameters is undefined, even
in C++.

Each parameter is a sequence point, you just do not know the
order of evaluation, which has nothing to do with the calling
convention (the calling convention is generally a standardization
within a particular runtime that includes details like order that the
parameters, return value, etc are placed onto the stack.

These are not specified and cannot be changed using just standard C++
Just as a trivia point, Java is defined as performing all operations in left-to-right order, without exception.

a = 5;
b = a++ + ++a;
In Java, this will work unambiguously, and b will be set to exactly 12.

a = func1() + func2();
In Java, func1() is guaranteed to be called before func2().

That would mean they have messed up the postfix operator and

b = a++ + a;

Would yield 11 for b in Java instead of the expected value of 10.


b = a + a++;   would still do what you expect.

A fine example of how Java took a feature from C and tried to "fix" it to be
unambiguous but wound up breaking it even worse and making a nice big
pitfall perfect  for catching C programmers trying to do something with Java.

Yeah, you don't need sequence points and that makes it a little simpler, but
that also makes it a gotcha of the language.

Sorry, you brought it up..
I feel like we're splitting hairs here, but I'll bite.

Where do you get your "expected" value of 10?  I think you are adding extra elements to the definition of post-increment that just are not there.  You're only guaranteed that the increment happens after the value is read, and before the next sequence point.  You're not guaranteed that the increment happens at the sequence point ( or after the expression, or whatever).

"b = a + a++;   would still do what you expect.".  That is not what I expect in C.  In C, there is absolutely no valid expectation for the value of this statement.  You might expect to get one of 2*a, 2*a+1, or 2*a+2, but the result of this expression is _undefined_.  You could get a completely random value and the compiler would still be standards conformant.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.