puts in c

What is puts in c programming?

puts is an output function which writes unformatted data to stdout.

puts is found in stdio header file.

Data is writen to standard output device that is console.

If you dont know about input output operations in c then read it now.

Prototype

#include<stdio.h>
int puts ( char * string );

Return type

This function returns int which the no of characters writen into console.

Parameters

One parameter that is string. The string we want to write to stdout.


Basic concepts about puts function

The parameter passed is a string that will be printed onto the console.
String in the sense the character array of C programming language.

This function reading from keyboard when it encounters new line character.
It even terminates even if you press enter button after inputting some data.

This function allows you to print a string including space.

In scanf even through space has a value, if scanf encounter space it ignores input after the space if input is specified as string. But this function does not ignore the space.

In printf of you have to specify the format specifier of the variable but puts does not allow you to include the other variable in it. In order to print other variables you can use a sprintf then convert it to a string then you can use gets to write out to stdout.

puts function moves the cursor to the next line after printing the string. If you don’t want the cursor move to the next line then you can use fputs function. Example is shown after the puts function example program.

Gets function does check for the array bound, hence it is not risky to use.


Puts function example program

//puts function in c 
#include<stdio.h>
int main(int argc, char const *argv[])
{
	char s[15];
	gets( s );
	puts(s);
	return 0;
}
puts in c

What are the differences between puts and printf?

Now we will look at the differences between puts and printf.

Puts writes out the unformatted data to console.Printf writes out the formatted data to the console.

No format specifier are required for puts.Format specifier is required for printf.

Puts writes the output string specified in the parameter.Printf prints according to the format string.

Look at the table below for simple words.

putsPrintf
Puts writes the unformatted data to cosole.Printf writes the formatted data to console.
Puts does not requires format specifiersPrintf requires format specifier
Puts prints out the string specified in parameter.Printf prints out according to the format string.
Implementation of this function is bit easy when compared.Implementation of this function is bit difficult when compared.
Puts moves the cursor to the next line after printing the data.Printf does not move the cursor to the new line after printing the data.

Read more on printf function in c programming.

You will learn how to use printf.

Read more on scanf function in c programming.

You will learn how to use scanf.

What are the differences between puts and putc?

lets look at the differences between putc and puts in c.

Function protoypes are one example.

putc writes out a character,where as puts writes out a string. we already know a string is array of characters.

To know more about strings please read this article on strings.


People also ask

What does puts do in C?

puts is an output function which writes unformatted data to stdout.

What is puts and gets in C?

These functions are found in the C standard library stdio.h. puts in c prints string to standard output console. gets reads a string from standard Input device that is keyboard.

What is the use of puts in C?

This function perform the output operation. In order to write string to the output console puts function is being used. Have a look on the above example to understand fluently.

how to use gets and puts in c programming

Simply include the standard input output header file internet header file you will find the definitions and declarations of these functions. After including the header file you can call the function name gets specified with a string as parameter in order to print out the string.
have look at the programming example explained above.

how puts works in c?

Function take string as parameter. Starts to read the string specified in the parameter character by character. Whenever it encounters \0 character stops printing out the data to the console. You may have a question in mind that why it has to stop whenever it encounters the \0 character because the string in C language ends with \0 character.

what does puts do in c?

puts performs the output operation. It writes string to the standard output console.


Practice more in C programming