true or false

This is a discussion on true or false within the C Programming forums, part of the General Programming Boards category; Hello all I'm a novice both in c programming and this forum this is a true or false question : ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    true or false

    Hello all

    I'm a novice both in c programming and this forum

    this is a true or false question :
    -----------------------------------------------------

    The following exrpession assigns 2 in x.

    (3 > 4 ) && ( x=2)
    -----------------------------------------------------
    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Think of how operator && behaves when its left hand operand is false. Does it go on to evaluate its right hand operand?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What laserlight is saying is . . .

    If the left side of a && expression is evaluated to false, it does not evaluate the second, right side, as it would be useless. If the left side is false, then the result is always false.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    ok guys i get your point

    so what if i change it a bit :

    ( 4>3 ) && ( x=2 )

    would that be true ?

    Meaning , if the very next line of code is

    printf("%d",x)

    will i get a

    2


    on the screen ?


    Thanks for all the help

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Well, if you tried it . . .

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int x = 0;
        (4>3) && (x = 2);
        printf("%i\n", x);
        return 0;
    }
    . . you get . . .

    Code:
    2
    So, then, yes, you are correct.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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