Search code examples
csettingsansi-c

are format "positional parameters" of printf(3) considered ansi C?


I am taking a C programing class and one of my assignments did not compile properly with my professors compiler.

My professor ended up giving me full credit after I explained my code to her, but requested that I use ansi C only. Hence my question.

here is the code I used.

    printf("The value of 2(%1$.1f)**3+3(%1$.1f)**2+4(%1$.1f) + 5 = %2$.1f\
     \n", y, x);

here is my compiler settings using xcode 4.5 https://i.sstatic.net/mUVjg.jpg

is this ansi C? or not? and if not how would I change it to be strict ansi C?

and also here is the bit about positional parameters from apples printf(3) man page.

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/printf.3.html

"

    Arguments are numbered starting at 1.
      If unaccessed arguments in the format string
     are interspersed with ones that are accessed the results will be indeterminate.

"


Solution

  • No, this is not ANSI C, not even C99. It's a POSIX extension. ANSI C does not provide any method for changing the order of the arguments to be printed. In your specific case, you'll need to pass the arguments multiple times:

    printf("The value of 2(%.1f)**3+3(%.1f)**2+4(%.1f) + 5 = %.1f\n", y, y, y, x);