CrazyEngineers
  • Nikumbh
    Nikumbh

    MemberJun 20, 2012

    C aptitude question

    Can anyone tell me how the following code results...

    #include

    int main()
    {
    int a=1,b=1,c;
    c=a=0? (a=1)😔b=2);
    printf("%d %d %d",a,b,c);
    getch();
    return 0;
    }
    Results as 2 2 2
    clarify me how the variables a , b , c holds the value 2?
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • nareshkumar6539

    MemberJun 20, 2012

    yes answer is 2 2 2
    assignment operator is right associative
    c=a=0? (a=1)😔b=2); will be divided into two parts
    so first of all
    a=0?(a=1)😔b=2); will be executed in that statement first a=1 and b=2 will be assigned.
    In conditional operator if condition is true first part will be executed otherwise second part will be executed
    0 means it is false so b value will be assigned to a.now a contains 2, after that next assignement will be take plase i.e c value will be replaced by a value i.e now c also contains 2.
    Are you sure? This action cannot be undone.
    Cancel
  • Nikumbh

    MemberJun 23, 2012

    @nareshkumar653 thanks a lot... 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register