C Programming/stdio.h/puts

From Wikibooks, open books for an open world
Jump to navigation Jump to search

puts is a function used to output a string (plus a newline), for example,

#include <stdio.h>
int main() {
    puts("welcome to WIKIPEDIA!!!");
}

output (on stdout):

welcome to WIKIPEDIA!!!

Differences from printf:

1. puts prints a newline after the supplied text

2. puts prints the string as is (it does not process % codes).

We can also pass a variable to puts, for example,

#include <stdio.h>
int main() {
    const char *str = "welcome to WIKIPEDIA!!!";
    puts(str);
}

output:

welcome to WIKIPEDIA!!!

puts has the following prototype:

int puts(const char *str)

It will print each byte at str until a null is reached, then print a newline. puts returns the number of bytes written (including the newline), or EOF (if an error occurred.)

To print a string without processing % codes, or outputting a newline, try:

    printf("%s", "welcome to WIKIPEDIA!!!");