In this article, we will learn about strings in the C language. We will learn how to print a string on a console in C language. Also, we will learn how to print each character of a string in C language.
- How to print a string in C language.
- How to print each character of a string in C language.
How to print a string in C language
In this example, we are printing a string on the console, by using the %s format specifier. This specifier is used to print a string variable.
C Program to Printing a String
#include <stdio.h>
int main() {
char str[] = "DevEnum Tutorials";
printf("For Best C Tutorials Visit: %s\n", str);
return 0;
}
Output
For Best C Tutorials Visit: DevEnum Tutorials
2. How to print each character of a string in C language
In this example, we are learning how to iterate over a string in C and then print each character of that string one by one. We are going to make use of for loop to iterate and then %c specifier to print each character of this string.Let us see how to do this with the help of below C code example.
C Program to Printing a String
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "DevEnum Tutorials";
for(int i = 0; i < strlen(str); i++)
printf("%c\n", str[i]);
return 0;
}
Output
D
e
v
E
n
u
m
T
u
t
o
r
i
a
l
s