An algorithm is given below to explain the process which is included in the C programming language to print the characters and strings in different formats.
Step 1: Read a character to print.
Step 2: Read a name at compile time.
Step 3: Output of characters in different formats by using format specifiers.
- printf("%c\n%3c\n%5c\n", x,x,x);
- printf("%3c\n%c\n", x,x);
- printf("\n");
Step 4: Output of strings in different formats by using format specifiers.
- printf("%s\n", name);
- printf("%20s\n", name);
- printf("%20.10s\n", name);
- printf("%.5s\n", name);
- printf("%-20.10s\n", name);
- printf("%5s\n", name);
Example
Following is the C program to print the characters and strings in different formats −
#include<stdio.h>
main(){
char x = 'T';
static char name[20] = "Tutorials Point";
printf("OUTPUT OF CHARACTERS\n\n");
printf("%c\n%3c\n%5c\n", x,x,x);
printf("%3c\n%c\n", x,x);
printf("\n");
printf("OUTPUT OF STRINGS\n\n");
printf("%s\n", name);
printf("%20s\n", name);
printf("%20.10s\n", name);
printf("%.5s\n", name);
printf("%-20.10s\n", name);
printf("%5s\n", name);
}Output
When the above program is executed, it produces the following output −
OUTPUT OF CHARACTERS T T T T T OUTPUT OF STRINGS Tutorials Point Tutorials Point Tutorials Tutor Tutorials Tutorials Point
Example
Consider another program to check different format −
#include<stdio.h>
main() {
char x = 'T';
static char name[20] = "Tutorials Point";
printf("OUTPUT OF CHARACTERS\n\n");
printf("%c\n", x);
printf("%c\n%3c\n%5c\n", x,x,x);
printf("%3c\n%c\n", x,x);
printf("%c\n%3c\n%5c\n", x,x,x);
printf("%3c\n%c\n", x,x);
printf("\n");
printf("OUTPUT OF STRINGS\n\n");
printf("%.5s\n", name);
printf("%-10.10s\n", name);
printf("%.20s\n", name);
printf("%20.10s\n", name);
printf("%20s\n", name);
printf("%10s\n", name);
}Output
When the above program is executed, it produces the following output −
OUTPUT OF CHARACTERS
T
T
T
T
T
T
T
T
T
T
T
OUTPUT OF STRINGS
Tutor
Tutorials
Tutorials Point
Tutorials
Tutorials Point
Tutorials Point