How does the comma acts here as an operator or as a separator?

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

  • simplycoder
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include 
    
    
        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 
    
    
    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
  • radha gogia
    radha gogia
    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include 
    
    
        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 
    
    
    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.
  • radha gogia
    radha gogia
    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include 
    
    
        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 
    
    
    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.
  • radha gogia
    radha gogia
    Sir ,Actually I have one con
    simplycoder
    Nice question, however, there is a typo in your snippet.
    The code should probably be
    #include 
    
    
        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 
    
    
    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.

You are reading an archived discussion.

Related Posts

OnePlus One has announced the price of 16GB Silk White model in India as Rs. 18,999. The model is 4G ready and offers impressive hardware and software features. The phone...
An Israeli startup named 'Sesame Enable' has designed a touch-free phone for the physically disabled people and is quickly taking the world by storm. A team of three, Oden Ben...
The perfect basket case. Quote: Comau Robotics has taken on Kuka at its own game of man versus machine and managed to put its own twist on it. The Italian...
CloudInfra is an Information Technology and Services company founded by Melchizedec Sundararaj in the year 2012. CloudInfra builds soft infrastructure services on the cloud. They specialize in Big Data, Distributed,...
Quote: For many scientists working in the field the thought of dragging around heavy and expensive equipment is simply unimaginable. Even the ever useful microscope may become a luxury they...