Difference between puts and printf in c language.

puts and gets
#include<stdio.h>   
#include<conio.h>
main()
{
	puts("I am a programmer");   //puts use as printf and gets use as scanf
	puts("Nesark World");        //check it puts or printf both yourself
	printf("Nesark World Again");
	getch();
}

puts and printf are both standard library functions in C that are used to output text to the screen. However, they have some differences:

  1. Syntax: The syntax of puts is simpler than that of printf. puts takes only one argument, which is the string to be printed, and automatically appends a newline character at the end. printf, on the other hand, takes a format string as its first argument, followed by a variable number of arguments that are used to replace the placeholders in the format string.
puts("Hello, World!"); // prints "Hello, World!" and a newline
printf("Hello, %s!", "World"); // prints "Hello, World!"
  1. Formatting: printf provides more advanced formatting options than puts. For example, printf allows you to control the width, precision, and alignment of the output, and to print numbers in different bases. puts does not have these formatting options.
  2. Return value: printf returns the number of characters printed, or a negative value if an error occurs. puts returns a non-negative value if successful, or EOF if an error occurs.
  3. Buffering: printf uses buffered output, which means that the output is not immediately written to the screen, but is stored in a buffer until the buffer is full or the program ends. This can make printf faster than puts in certain situations. puts however writes the string immediately to the console and starts a new line after.

In general, puts is simpler to use and is suitable for quick and simple output tasks, while printf provides more advanced formatting options and is more suitable for more complex output tasks that require more control over the layout and appearance of the output.

Thanks

Difference between puts and printf in c language.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top