Thread: operators

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    operators

    Hi i am ujwala i have a confusion regarding inc and dec operators(pre and post).i mean how r they evaluated if they are given in a statemnt.
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i=0;
    int k;
    clrscr();
    k=(i++)-(i++)-(--i)-(++i);
    printf("%d\n",i);
    printf("%d\n",k);
    getch();
    }
    here i calculated in this way plz clear me if i am wrong
    now i value is evaluated from left to right
    here i got i=2,k=0 which is same as in o/p window

    but for this k=(i++)+(i++)+(--i)+(++i);
    i got k=2,i=2 but here teh o/p window shows i=2,k=0 how??i dnt understand

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot say which way i evaluates. i will get incremented three times and decremented once, for a final value of 2, but the value of k is Indeterminate. The fact that you get a compiled program at all is a miracle in and of itself.

    Also: no void main(), but int main().

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I hope you actually indent code and this is just a sample with no indentation.
    As for question:
    Code:
    int i = 0;
    printf("&#37;d", i++); /* Prints 0, then increments i */
    printf("%d", i); /* Prints 1 */
    printf("%d", ++i); /* Increments i, then prints 2 */
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://c-faq.com/expr/evalorder2.html
    The basic problem is, if you modify the same thing twice with side-effects between sequence points (see that FAQ for a definition), then the whole thing is undefined.

    At that point, it's useless to try and understand what's going on so you may as well just sit back and admire the light show for what it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we break
    Code:
    k=(i++)-(i++)-(--i)-(++i);
    into how it gets evaluated:
    k =
    (i++)-(i++)
    The problem here is that the compiler is allowed to do:
    temp1 = i;
    i++;
    temp2 = i;
    i++;
    temp3 = temp1 - temp2
    or
    temp1 = i;
    temp2 = i;
    i++;
    i++;
    temp3 = temp1 - temp2

    The rest of the calculation has the same problem.

    Of course, the result will be different in these two cases. The C standard allows this to be implemented as the compiler likes so that it can be done efficiently by the compiler for "normal cases".

    By the way, it would probably be more efficient to write:

    Code:
    k=(i)-(i+1)-(i)-(i+1); i+=2;
    Not to mention that you'd know what is actually going on [and the compiler could reduce it to:
    Code:
    k = -2; i += 2;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM