简体   繁体   English

printf(3)的格式“位置参数”是否被认为是ansi 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. 我正在参加一个C编程课程,我的一个任务没有与我的教授编译器正确编译。

My professor ended up giving me full credit after I explained my code to her, but requested that I use ansi C only. 在我向她解释我的代码后,我的教授最终给了我充分的信用,但要求我只使用ansi C. 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 http://i.imgur.com/KtMuSJd.jpg 这是我使用xcode 4.5的编译器设置http://i.imgur.com/KtMuSJd.jpg

is this ansi C? 这是ansi C吗? or not? 或不? and if not how would I change it to be strict ansi C? 如果不是,我将如何将其更改为严格的ansi C?

and also here is the bit about positional parameters from apples printf(3) man page. 这里还有关于apple printf(3)手册页中位置参数的信息。

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/printf.3.html 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.

"

No, this is not ANSI C, not even C99. 不,这不是ANSI C,甚至不是C99。 It's a POSIX extension. 这是POSIX扩展。 ANSI C does not provide any method for changing the order of the arguments to be printed. ANSI C不提供任何方法来更改要打印的参数的顺序。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM