C Programming/C Reference/stdio.h/fputs

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

fputs is a function in C programming language that writes an array of characters to a given file stream. fputs stands for file put string. It is included in the C standard library header file stdio.h.

The function fputs terminates after reaching terminating null character ('\0'). The null character is not copied to the stream. The prototype of the function is as follows:

int fputs ( const char * str, FILE * stream );

The stream argument specifies the stream to which the string will be written. stdout is commonly used here, for writing to the standard output. Otherwise, a FILE * value returned by the fopen() function is used.

Sample usage[edit]

The following example is 'hello world' program using fputs:

#include <stdio.h>
 
int main()  {
    const char *buffer = "Hello world!";
    fputs (buffer, stdout);
    return 0;
    }

The following is a program that asks the user for his name and then writes it out:

#include <stdio.h>
int main() {
    char name[50];
    fputs("What is your name? ", stdout);
    fgets(name, sizeof(name), stdin);
    fputs(name, stdout);
    return 0;
    }

External links[edit]