Home / Programs / Write a program that demonstrates the variety of ways that a string can be printed.
Programming Example

Write a program that demonstrates the variety of ways that a string can be printed.

👁 851 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that demonstrates the variety of ways that a string can be printed.

Program Code

#include <stdio.h>

int main()
{
  char s[]="Hello, World";

  printf(">>%s<<\n",s);
  printf(">>%20s<<\n",s);
  printf(">>%-20s<<\n",s);
  printf(">>%.4s<<\n",s);
  printf(">>%-20.4s<<\n",s);
  printf(">>%20.4s<<\n",s);
  return 0;

}

Output

>>Hello, World<<
>>        Hello, World<<
>>Hello, World        <<
>>Hell<<
>>Hell                <<
>>                Hell<< 

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.