-4

Trying to print a string before and after a variable. Does C have the capability to use one statement to display this output?

This works:

float value = 5;
printf("\nThe value of %f", value);
printf(" is greater than zero.");

This is the desire (one statement):

float value = 5;
printf("\nThe value of %f", value, " is greater than zero.");

(Second string does not display)

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • 12
    Why not simply `printf("The value of %f is greater than zero.\n", value)`? – Some programmer dude Sep 28 '16 at 19:57
  • 2
    Please note that `'\n'` is the end of line character, putting it at the start is kind of wierd. – Iharob Al Asimi Sep 28 '16 at 19:58
  • 1
    Please refer the syntax of printf() – Dilip Kumar Sep 28 '16 at 19:58
  • 1
    By the way, note that in my previous comment i put the newline at the *end* of the string to be printed. That's because `stdout` (which `printf` writes to) is by default *line buffered*, which means that the text will be written to the output device on newline. Therefore will printing the newline first print the *previous* line but not the current (until next newline which may take a while and lead to "missing" output). – Some programmer dude Sep 28 '16 at 19:59
  • @JoachimPileborg Thanks for the help! – user2884536 Sep 28 '16 at 20:04
  • @JoachimPileborg Concerning buffering, suggest that "by default line buffered which means that the text will be written to the output device on newline", although common, is not specified by C. See [What are the rules of automatic flushing stdout buffer in C?](http://stackoverflow.com/q/39536212/2410359) – chux - Reinstate Monica Sep 28 '16 at 20:07
  • Hey @user2884536 if my answer (or anyone's) has solved your problem, please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Thank you! – MD XF Oct 29 '16 at 21:52

2 Answers2

9

You're thinking of something like Java. In C you can do this:

printf("The value of %f is greater than zero.\n", value);
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
MD XF
  • 7,860
  • 7
  • 40
  • 71
4

The printf() function takes one format string and zero or more additional arguments to plug into that format. @MDXF presented the most obvious approach to producing the output you want with a single printf(), but an important alternative is

printf("\nThe value of %f%s", value, " is greater than zero.");

Note there the second field descriptor in the format string (%s) that specifies where and how to format the second argument following the format. It's kinda silly to do it that way when the string is given as a literal, but that would be the natural way to do it if you instead had a char * pointing to the string to print, which is a common case:

void printit(double value, const char *string) {
    printf("\nThe value of %f%s", value, string);
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157