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‬<stdio.h>

  • 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

Welcome, guest

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

CrazyEngineers powered by Jatra Community Platform

  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Feb 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

  • radha gogia

    radha gogia

    @radha-BTDzli Feb 25, 2015

    Sir ,Actually I have one con

    simplycoderNice 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.

  • radha gogia

    radha gogia

    @radha-BTDzli Feb 25, 2015

    Sir ,Actually I have one con

    simplycoderNice 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.

  • radha gogia

    radha gogia

    @radha-BTDzli Feb 25, 2015

    Sir ,Actually I have one con

    simplycoderNice 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.