C aptitude question

Nikumbh

Nikumbh

@nikumbh-lmTCgS Oct 25, 2024
Can anyone tell me how the following code results...

#include<stdio.h>

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • nareshkumar6539

    nareshkumar6539

    @nareshkumar6539-BKuVbx Jun 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.
  • Nikumbh

    Nikumbh

    @nikumbh-lmTCgS Jun 23, 2012

    @nareshkumar653 thanks a lot... 😀