#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Question on order of execution for printf


    I got an unexpected result from this code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define LENGTH 30
    
    int main()
    {
        char s1[LENGTH] = "Jupiter ", s2[LENGTH] = "Symphony";
        printf("%d %d\n", strlen(s1), strlen(strcat(s1, s2)));
        printf("%s", s1);
    }
    I expected the first line of output to be
    8 16.
    However, what I got was
    16 16.
    Does this mean that strcat was executed before the compiler substituted strlen(s1) for the first placeholder?

    I tried another piece of code to learn about the behavior of substitutions in printf.
    Code:
    #include <stdio.h>
    #include <string.h>
    #define LENGTH 30
    
    int main()
    {
        int i1, i2;
        i1 = 2;
        printf("%d %d\n", i1, ++i1);
    }
    I got 3 3 for the output instead of 3 3.

    It seems that printf executes the functions not in the format string all at once then does the substitutions, instead of executing the function and substituting one by one for each placeholder.
    Is this the case?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    In general as I understand it, printf and other functions start its evaluation of their arguments with the last, right-most argument and works its way left to the first, left-most argument. Since I'm at home, I cannot check the standard to see if that is specified there.

    Other languages, such as Pascal, evaluate the arguments from left to right. Windows does (or did back in v3.x) the same, going from left to right, even though it was written in C, as I understand. As a result, when writing Windows applications you needed to use the PASCAL qualifier to get your program to follow that convention; I think that's been encapsulated in a new qualifier, WIN_API or WINAPI.

    At any rate, you should never write a program that depends on the order in which arguments are evaluated, which is just like evaluating (x + x * ++x) (see my page on that at http://pgm.dwise1.net/misc/test.html) or other such idiocies that continually crop up here as homework assignments. You are getting into undefined behavior there, which someone on this forum likens to dancing barefoot on broken glass.

    Read up on sequence points.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    In general as I understand it, printf and other functions start its evaluation of their arguments with the last, right-most argument and works its way left to the first, left-most argument. Since I'm at home, I cannot check the standard to see if that is specified there.
    I see.

    At any rate, you should never write a program that depends on the order in which arguments are evaluated
    So in my case, executing strncat outside of printf and replacing the strncat with s1 would be a much better solution.

    Read up on sequence points.
    Done. Thanks for pointing me to the resource.

IMN logo majestic logo threadwatch logo seochat tools logo