Working of printf statement in C

I am just confused with the working of the printf statement that which one evaluate first because I have studied that comma separating the arguments acts like a separator and hence the order of evaluation of the arguments depends completely on the compiler ,so then here why the output on each compiler is "hellokill"
void func(int x, int y){}
main()
{
func(printf("kill"),printf("hello"));
getch();
}
So what is the reason that the right printf value gets printed here then left one executes.

Replies

  • Ankita Katdare
    Ankita Katdare
    evaluation of the arguments depends completely on the compiler
    Your answer lies in this statement that you wrote.

    The output of your program will completely depend on your compiler.
    For example, a function call like below may very well behave differently from one compiler to another:

    void func (int, int);
    int i = 2;
    func (i++, i++);

    Output could be either: '2, 3' or '3, 2'

    It is never safe to depend on the order of evaluation.
  • radha gogia
    radha gogia
    Ankita Katdare
    Your answer lies in this statement that you wrote.

    The output of your program will completely depend on your compiler.
    For example, a function call like below may very well behave differently from one compiler to another:

    void func (int, int);
    int i = 2;
    func (i++, i++);

    Output could be either: '2, 3' or '3, 2'

    It is never safe to depend on the order of evaluation.
    But Mam ,I studied that we need to follow the standard the function arguments are executed from right to left ,so although I too agree with this fact that it is completely dependent on compiler ,but what if I get any MCQ so then on which option to click depending on the right to left order evaluation or unspecified option.
  • Ankita Katdare
    Ankita Katdare
    I have no idea about MCQs asked on this topic. If they are asking a question and the options are one of the above, I guess the question setter isn't doing a good job of it.

    Will leave rest of it to experts. #-Link-Snipped-# #-Link-Snipped-#
  • Anoop Kumar
    Anoop Kumar
    From #-Link-Snipped-#,
    C/c++ compiler executes arguments form right to left. #-Link-Snipped-#
    The parameters are pushed from right to left (so that the first parameter is nearest to top-of-stack), and the caller cleans the parameters.
    So while pushing from right to left, you will get right one to print first.
    If you want to go deeper in subject. Stack and Calling (GNU Compiler Collection (GCC) Internals)
  • simplycoder
    simplycoder
    It all depends on how the compiler is constructed and is not fixed for all the languages.
    A similar doubt was raised here, which can help you better.
    #-Link-Snipped-#

You are reading an archived discussion.

Related Posts

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‬...
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,...