-2

I'm having trouble printing out this string in C

char *myXMLString = 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<painting>"
"   <img src=\"madonna.jpg\" alt=\"Foligno Madonna, by Raphael\"/>"
"   <caption>MY_REPLACED_TEXT"
"   <date>1511</date>-<date>1512</date>.</caption>"
"</painting>";

If I use printf("%c",*myXMLString); I get expected parameter declarator missing. Tried sprintf with similar results.

Ke.
  • 2,484
  • 8
  • 40
  • 78
  • `myXMLString` is a C-style string, not a single character. Therefore, you cannot use `%c` for that (unless you only need to print the first character of that string). In this case, you could do `printf(myXMLString);` or if you need to add other data, for example a newline you could use `printf("%s\n", myXMLString);`. You can find more background here: [Why does C's printf format string have both %c and %s?](https://stackoverflow.com/questions/10846024/why-does-cs-printf-format-string-have-both-c-and-s) – Elijan9 Sep 12 '16 at 08:34
  • By the way, myXMLString should be declared a `const char*`, since it points to a string constant rather than a writable character array. Otherwise you should use `char myXMLString[] = ...` – Elijan9 Sep 12 '16 at 08:39

2 Answers2

2
printf("%s", myXMLString);

is how you print a C-string. %s is the format to be used for priting strings. See the man page of printf for details.

On the other hand, if you could't figure this out yourself, you really need to study a proper textbook or at least a tutorial.

P.P
  • 117,907
  • 20
  • 175
  • 238
1
  fprintf(adgfile, "%s", myXMLString);
Serge
  • 6,088
  • 17
  • 27