. Advertisement .
..3..
. Advertisement .
..4..
You can print char array in C with the function printf(). Keep reading to learn how to use it.
Print Char Array in C
With printf()
In C, an array of characters is basically a string. But unlike other higher-level programming languages, C doesn’t have a class and associated methods for strings. There is no easy way to print a string in C as a result.
The best solution here is to use the function printf()
from the stdio.h header. This name means “print formatted”, meaning you will have to manually use control parameters to indicate the location to print your string into a stdout. This fixed format and its simple functionality are low-level compared to languages like Python or Java. However, for our purpose here, it should be enough.
This is the declaration for the printf()
function:
int printf(const char *format, ...)
The parameter format is a string that has the text you want to write to stdout. You can embed as many format tags as you need to this parameter to insert certain values. These tags also determine those values will be formatted when written into the output.
This is the prototype of each format tag:
%[flags][width][.precision][length]specifier
Here are some important specifiers you will need to remember:
- c: character
- i or d: signed decimal integer
- f: decimal floating-point numbers
- s: string of characters
- p: pointer address
- n: nothing printed
To print a string in C, you will need to use the s specifier.
Example:
#include<stdio.h>
int main()
{
char str[] = "ITTutoria.net";
printf("%s\n", str);
return 0;
}
Output:
ITTutoria.net
The above program declares an array of characters and assigns a string to it. The printf()
function is used to write this string to the output. Let’s look at its parameters in more detail. You have a format tag %s
, which indicates a string of characters. It will be replaced with the str array (that is why we put it after the text and a comma). The end of our text is \n
, or a new line.
With for loops
In addition to using the printf()
function and its format tags, you can be traditional and use a for loop to print a string in C. The algorithm is quite straightforward. As we go through each character of the array, we print it until we reach the end of the string.
#include<stdio.h>
#include<string.h>
int main() {
char str[] = "ITTutoria";
int len = strlen(str);
int i;
for (i = 0; i < len; i++) {
printf("%c", str[i]);
}
return 0;
}
Output:
ITTutoria
First, we need to get the length of our string to know the range of our loop. The string.h header provides the strlen()
function for this exact purpose.
As we go through each character in the string, we write it to the output with print()
. Keep in mind that this time around, we need to use the specifier c (for characters), not s (for strings of characters). Arrays in C are zero-indexed. That is why we use to start the index from 0, not 1, and use it to indicate the members of the array.
Conclusion
You can print char array in C with the function printf()
of the stdio.h
header file. Remember that it only offers simple capabilities, and you will need to learn format tags to control the output. Read this guide if you want to initialize a C array to 0.
Leave a comment