String Output

Again, we will use library functions. C has three standard library functions for printing strings. They are puts(), fputs(), and printf().

The puts() Function

The puts() function is very easy to use. Just give it the address of a string for an argument. Listing 11.8 illustrates some of the many ways to do this.

Listing 11.8. The put_out.c Program
/* put_out.c -- using puts() */
#include <stdio.h>
#define DEF "I am a #defined string."
int main(void)
{
  char str1[80] = "An array was initialized to me.";
  const char * str2 = "A pointer was initialized to me.";

  puts("I'm an argument to puts().");
  puts(DEF);
  puts(str1);
  puts(str2);
  puts(&str1[5]);
  puts(str2+4);
  return 0;
}

The output is this:

 I'm an argument to puts(). I am a #defined ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.