CrazyEngineers
  • Working of printf statement in C

    radha gogia

    radha gogia

    @radha-BTDzli
    Updated: Oct 26, 2024
    Views: 1.4K
    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.
    0
    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
  • Ankita Katdare

    AdministratorFeb 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.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberFeb 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Ankita Katdare

    AdministratorFeb 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-#
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberFeb 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>
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberFeb 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-#
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register