Home / Programs / Write a program that demonstrates the way the sprintf( ) and puts( ) library functions work with strings.
Programming Example

Write a program that demonstrates the way the sprintf( ) and puts( ) library functions work with strings.

👁 829 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that demonstrates the way the sprintf( ) and puts( ) library functions work with strings.

Program Code

#include <stdio.h>

int main()
{
  char buf[128];
  double x = 1.23456;
  int i = 0;
  
  sprintf(buf,"x = %7.5lf",x);
  
  while(i<10)
    puts(buf+i++);
	
  return 0;
}

Output

x = 1.23456
 = 1.23456
= 1.23456
 1.23456
1.23456
.23456
23456
3456
456
56 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.