w3resource

C puts() function

C library function - puts()

The puts() function is used to write specified string (plus a newline), to the standard output stream. The ending null character is not written.

Syntax:

int puts(const char *str)

Parameters:

Name Description Required /Optional
str This is the string to be written. Required

Return value

  • Upon successful completion, puts() shall return a non-negative number.
  • Otherwise, it shall return EOF, shall set an error indicator for the stream, and errno shall be set to indicate the error.

Example: puts() function

#include <stdio.h>
int main() 
{
    const char *text = "C Programming.";
    puts(text);
    return 0;
}

Output:

C Programming.

Difference from printf()

  • The puts() function prints a newline after the text supplied.
  • The puts() function prints the string as it is (% codes are not processed).

C Programming Code Editor:

Previous C Programming: C putchar()
Next C Programming: C perror()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Reading a string with scanf :

An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.

Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.

Ref : https://bit.ly/3pdEk6f