CrazyEngineers
  • plz explain the working of below code ,here why both the printf functions are getting executed when comma is here acting like an operator and not like a separator
    #‎include‬
    • int main()
    • {
    • int i;
    • int g= i=8?(printf("hello"), printf("life")), printf("kill");
    • printf("%d",g);
    • return 0;
    • }
    • I am confused with the fact that when I write int a=(8,9) so the value assigned to a is 9 because the comma acts here as an operator,then why does here both the printf's are getting executed.
    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
  • simplycoder

    MemberFeb 25, 2015

    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include <stdio.h>
    
    
        int main()
    
        {
    
        int i;
    
        int g= i=8?(printf("AB"), printf("C")): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    In this case, your assignment operator will give it a true value and will execute the true part in normal left to right format.
    printing ABC1
    Now lets modify this code a bit

    #include <stdio.h>
    
    
    int func(int x,int y)
    {
        return -1;
    }
        int main()
    
        {
    
        int i;
    
        int g= i=8?(func(printf("AB"), printf("C"))): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    This would execute and print the result as CAB-1

    I hope this would help.

    Regards,
    -SC
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberFeb 25, 2015

    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include <stdio.h>
    
    
        int main()
    
        {
    
        int i;
    
        int g= i=8?(printf("AB"), printf("C")): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    In this case, your assignment operator will give it a true value and will execute the true part in normal left to right format.
    printing ABC1
    Now lets modify this code a bit

    #include <stdio.h>
    
    
    int func(int x,int y)
    {
        return -1;
    }
        int main()
    
        {
    
        int i;
    
        int g= i=8?(func(printf("AB"), printf("C"))): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    This would execute and print the result as CAB-1

    I hope this would help.

    Regards,
    -SC
    Sir ,Actually I have one confusion regarding the evaluation of arguments of the function ,I have studied that it completely depends on the compiler whether left to right or right to left ,but I have seen in some compilers that they support the evaluation of arguments form right to left ,so is there some standard followed that we evaluate the function arguments from right to left,and is there any advantage associated with it.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberFeb 25, 2015

    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include <stdio.h>
    
    
        int main()
    
        {
    
        int i;
    
        int g= i=8?(printf("AB"), printf("C")): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    In this case, your assignment operator will give it a true value and will execute the true part in normal left to right format.
    printing ABC1
    Now lets modify this code a bit

    #include <stdio.h>
    
    
    int func(int x,int y)
    {
        return -1;
    }
        int main()
    
        {
    
        int i;
    
        int g= i=8?(func(printf("AB"), printf("C"))): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    This would execute and print the result as CAB-1

    I hope this would help.

    Regards,
    -SC
    Sir ,Actually I have one confusion regarding the evaluation of arguments of the function ,I have studied that it completely depends on the compiler whether left to right or right to left ,but I have seen in some compilers that they support the evaluation of arguments form right to left ,so is there some standard followed that we evaluate the function arguments from right to left,and is there any advantage associated with it.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberFeb 25, 2015

    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include <stdio.h>
    
    
        int main()
    
        {
    
        int i;
    
        int g= i=8?(printf("AB"), printf("C")): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    In this case, your assignment operator will give it a true value and will execute the true part in normal left to right format.
    printing ABC1
    Now lets modify this code a bit

    #include <stdio.h>
    
    
    int func(int x,int y)
    {
        return -1;
    }
        int main()
    
        {
    
        int i;
    
        int g= i=8?(func(printf("AB"), printf("C"))): printf("kill");
    
        printf("%d",g);
    
        return 0;
    
        }
    
    This would execute and print the result as CAB-1

    I hope this would help.

    Regards,
    -SC
    Sir ,Actually I have one confusion regarding the evaluation of arguments of the function ,I have studied that it completely depends on the compiler whether left to right or right to left ,but I have seen in some compilers that they support the evaluation of arguments form right to left ,so is there some standard followed that we evaluate the function arguments from right to left,and is there any advantage associated with it.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register