logical expression question

This is a discussion on logical expression question within the C Programming forums, part of the General Programming Boards category; code: i = 7; j = 8; k = 9; printf("%d", (i = j) || (j = k)); printf("\n"); printf("%d ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    logical expression question

    code:
    i = 7; j = 8; k = 9;
    printf("%d", (i = j) || (j = k));
    printf("\n");
    printf("%d %d %d", i, j, k);

    ... i get
    1
    7 8 9
    ...why is the answer:
    1
    8 8 9
    ?

    also...
    code: i = 1; j = 1; k = 1;
    printf("%d", ++1 || ++j && ++k);
    printf("\n");
    printf("%d %d %d", i, j, k);

    ...i get
    1
    1 1 1

    .... why is the answer:
    1
    2 1 1

    ??

    basically i'm wondering WHY 'i' is the only one being recalibrated (and what the reason for that is)

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: logical expression question

    Originally posted by JohnMayer
    [B]
    Code:
    i = 7; j = 8; k = 9;
    printf("%d", (i = j) || (j = k));
    printf("\n");
    printf("%d %d %d",  i,  j, k);
    Simple, you have

    printf( "%d", (i = j) || (j = k));

    Clearly you wanted to use Logical Comparisons. For that you need DOUBLE equals, to check equality... What you are doing in your printf is actually setting those values, not checking them. So try this

    printf( "%d", (i == j) || (j == k));

    Not sure what you even want to do here? Did you mean i for the first argument or 1? Also using some paranthesis would clear things up.
    printf("%d", ++1 || ++j && ++k);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing an expression, strtok or other alternatives?
    By _Marcel_ in forum C Programming
    Replies: 3
    Last Post: 03-04-2009, 07:57 AM
  2. Expression Tree
    By Coding in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2008, 04:24 PM
  3. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21