Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is puts in C?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The puts function in C is used to write a line or string to the output stream (stdout) that is up to, but does not include, the null character. The puts function also appends a newline character to the output and returns an integer.

To use the puts function, you need to include the <stdio.h> library in the program. This is shown below:

#include <stdio.h>

The prototype of the puts function is shown below:

int puts(const char* str);

Parameters

The puts function takes a single mandatory parameter, i.e., a null-terminated character array.

Return Value

The puts function writes the provided argument to the output stream and appends a newline character at the end.

If the execution is successful, the function returns a non-negative integer; otherwise, it returns an EOF (End-of-File) for any error.

Example

The code below shows how the puts function works in C:

#include <stdio.h>
#include <string.h>

int main() {
 
  // initializing strings
  char str1[] = "Hello World";
  char str2[] = "Using puts in C";

  // writing to stdout
  puts(str1);
  puts(str2);

  return 0;
}

Explanation

First, two character arrays (str1 and str2) are initialized. These arrays are then provided as arguments to the puts function called on Lines 11 and 12.

The puts function proceeds to write the contents of the strings to stdout and appends a newline character to each string. As a result, each string is printed on a separate line.

RELATED TAGS

c

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring