Thread: very simple but tricky... please help

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    38

    very simple but tricky... please help

    When i run the following code
    main()
    {
    int i=-3,j=2,k=0,m;
    m=++i&&++j||++k;
    printf("\n %d %d %d %d " ,i,j,k,m);
    }

    the result i get is

    -2 3 0 1

    What i'm not able to understand is that why the value of k is not incremented??

    I was trying different things with the expression , i noticed that if i use m=++i&&++j&&++k; instead of the original expression the result is

    -2 3 1 1

    i.e. this time k has been incremented ...WHY??

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    My best gues is that the system is doing early termination of the conditionals. Once the ++i&&++j resolve to true, it doesn't matter what k is because the result and 1 || (1 or 0) is always 1. So the check is terminated and k is never incremented. When you replace || with &&, the system must evaluate k to determine the results.

    PK

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > My best gues is that the system is doing early termination of the conditionals.
    You are right, but you shouldn't be guessing such things.

    > m=++i&&++j||++k;
    Which is why in most programming standards (and professional code), mixing operators with side effects with other operators is avoided.
    Sure it's legal code, but if you have to sit and think about it for several minutes each time you see it, it's hardly productive code.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Well yeah, but even brain dead monkey who just learned how to program wold think that
    Code:
    m = ++i && ++j || ++k;
    wold be replaceble with
    Code:
    i++;
    j++;
    k++;
    m = i && j || k;
    But it isn't! I blame god!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by iwabee
    Well yeah, but even brain dead monkey who just learned how to program wold think that
    Code:
    m = ++i && ++j || ++k;
    wold be replaceble with
    Code:
    i++;
    j++;
    k++;
    m = i && j || k;
    But it isn't! I blame god!
    Doesn't learning how to program involve reading? Every C book I've ever read talks about early termination with conditional operators (if the book talks about conditional operators).
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Ok monkey was bad example, but I wasn't talking about early termination of whatever. Rather of the whole concept of increment/decrement operators. Increment before/after use VS. increment before/after statement.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    38
    Thanx to all........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM