Working of printf statement in C

radha gogia

radha gogia

@radha-BTDzli Oct 26, 2024
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

Welcome, guest

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

CrazyEngineers powered by Jatra Community Platform

  • Ankita Katdare

    Ankita Katdare

    @abrakadabra Feb 22, 2015

    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

    @radha-BTDzli Feb 23, 2015

    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

    @abrakadabra Feb 23, 2015

    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

    @anoop-kumar-GDGRCn Feb 23, 2015

    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. <a href="https://gcc.gnu.org/onlinedocs/gccint/Stack-and-Calling.html" target="_blank" rel="nofollow noopener noreferrer">Stack and Calling (GNU Compiler Collection (GCC) Internals)</a>
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Feb 25, 2015

    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-#