char letters[] = { 'a','b','c','d' }; printf ("%.*s", 3, letters); Output is: abc Answer from __Punk-Floyd__ on reddit.com
๐ŸŒ
Linux Hint
linuxhint.com โ€บ print-char-array-through-printf-c-programming
How to Print a Char Array in C Through printf โ€“ Linux Hint
Then inside the main() function define the array type as char (character type). After that to print the array, you can use the for or while loop that are useful in printing the elements of the array one by one through printf.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ print char array in c
How to Print Char Array in C | Delft Stack
February 2, 2024 - In this article, we explored several methods for printing character arrays in C. We discussed using loops, the %s specifier with printf, and the puts function.
Discussions

How to print a char array in C through printf? - Stack Overflow
Also you can initialize your char arrays with string literals too: char mystring[6] = "alien"; //the zero is put by the compiler at the end ยท Also: Functions that operate on C Style Strings (e.g. printf, sscanf, strcmp,, strcpy, etc) need zero to know where the string ends More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - I can't print char array's elements with for loop - Stack Overflow
I put my i into char's second dimension, is it not true? Basically, I want to print all the elements in my array, how can I do it? Thanks in advance. ... printf("%s\n",usernames[i]); is what you want. Keep in mind that your array only has 5 items in it. Once you go past 4 in your loop you've ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
C - print array of char * - Stack Overflow
I am trying to read thousands of words and print only the first 15 but the only word that it prints is the very last one in the array where I stored the words. g++ --version g++ (Ubuntu/Linaro 4.6.3- More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 27, 2017
c - Printing an array of characters - Stack Overflow
@liangteh In C all the string must be null terminated, so the last character must be a '\0' (a char of value 0) 2011-10-23T10:50:32.753Z+00:00 ... there is no way I can use printf to print the array except for looping the array and check for NULL ? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/cprogramming โ€บ [question] how to print multiple chars from array?
r/cprogramming on Reddit: [question] How to print multiple chars from array?
August 12, 2021 -

i want to be able to print out multiple values from an array without having to re-write the char name and array number multiple times, is this possible? example...

char letters[] = {'a','b','c','d'};
printf("%c%c%c%c", letters[0], letters[1], letters[2]);

i want to make that code cleaner and easier to use/understand. I thought i'd be able to use

printf("%c", letters[0][1][2]);

or seperated by commas, but that doesn't work either. Any suggestions?

๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 21302-how-do-you-create-print-array-characters.html
How do you create and print an array of characters
July 9, 2002 - char bufferarray[31]; int i; for(i=0;i<31;i++) bufferarray[i]='\45'; bufferarray[31]='\0'; printf("%s \n",bufferarray); the second loop is not requried Also , most string manipulation functions declared in string.h expect NUL terminated character arrays . The one who says it cannot be done ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to print string in c
How to Print String in C - Scaler Topics
April 16, 2024 - ... Explanation: We have initialized ... used a while loop statement to iterate over the array till the null character is encountered and simultaneously printed every element of the character array....
Find elsewhere
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 468151 โ€บ c-programming-print-char-array-on-separate-line-for-each-element
c programming print char array on separate line for each element
November 22, 2013 - Your first example should work if the for loop terminates when i = 5. char str1[5] = "code"; char str2[5] = "code"; for(i; i < 5; i++) { printf("The array length is %c %c :\n", str1[i], str2[i]); } โ€” gerard4143 371 Jump to Post
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-to-print-characters-in-a-string
C Program to Print Characters in a String
December 19, 2024 - This string Characters program is same as above. Here, we just replaced the while loop with For Loop. /* C Program to Print Characters in a String */ #include <stdio.h> int main() { char str[100]; printf("\n Please Enter any String : "); scanf("%s", str); for(int i = 0; str[i] != '\0'; i++) { printf("The Character at %d Index Position = %c \n", i, str[i]); } return 0; }
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ how-to-print-each-element-of-a-c-string-array-136081
How to Print Each Element of a C String Array in C | LabEx
January 22, 2026 - In this example, we declare the names array as an array of pointers to char. We then use pointer arithmetic to access each element of the array and print it using the printf() function.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-print-out-the-arrays-of-character-as-a-single-word-or-a-string-in-the-C-language
How to print out the arrays of character as a single word or a string in the C language - Quora
If I want to try out some ideas, ... be of value. ... for a a null terminated array of char, puts(a) puts the array out. printf(โ€œs\nโ€, ......
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ program to print array in c
Program to Print Array in C - Scaler Topics
April 4, 2024 - In the above code, we print an array in C with the help of a while loop. We create an array arr initially and then we use a while loop with the condition that the variable i which is initialized with 1 should be less than the size of the array.
Top answer
1 of 4
12

Given that the code is really simple, I see mostly coding style issues with it.

Instead of this:

char *names[] = {
    "John", "Mona",
    "Lisa", "Frank"
};

I would prefer either of these writing styles:

char *names[] = { "John", "Mona", "Lisa", "Frank" };

// or

char *names[] = {
    "John",
    "Mona",
    "Lisa",
    "Frank"
};

The pNames variable is pointless. You could just use names.

Instead of the while loop, a for loop would be more natural.

This maybe a matter of taste, but I don't think the Hungarian notation like *pArr is great. And in any case you are using this pointer to step over character by character, so "Arr" is hardly a good name. I'd for go for pos instead. Or even just p.

You should declare variables in the smallest scope where they are used. For example *pos would be best declared inside the for loop. In C99 and above, the loop variable can be declared directly in the for statement.

The last return statement is unnecessary. The compiler will insert it automatically and make the main method return with 0 (= success).

Putting it together:

int main(int argc, char *argv[])
{
    char *names[] = { "John", "Mona", "Lisa", "Frank" };
    for (int i = 0; i < 4; ++i) {
        char *pos = names[i];
        while (*pos != '\0') {
            printf("%c\n", *(pos++));
        }
        printf("\n");
    }
} 

Actually it would be more interesting to use argc and argv for something:

int main(int argc, char *argv[])
{
    for (int i = 1; i < argc; ++i) {
        char *pos = argv[i];
        while (*pos != '\0') {
            printf("%c\n", *(pos++));
        }
        printf("\n");
    }
} 
2 of 4
9

Other points not already mentioned:

Use const where possible

It's better to use const where possible so that it's clear when you are intending to modify things and clear when you're not. This helps the compiler help you find bugs early.

Avoid magic numbers

It's better to avoid having numbers like 4 in the program. If you want to change things later, you may well be left wondering "why 4? what does that mean?" Better would be to assign a constant with a meaningful name.

Use putchar rather than printf for single characters

The printf function is very useful, but putchar is often better suited for single-character output. The reason is that printf has to, at runtime, parse the format string, apply the arguments and then emit the results, while putchar only has to pass a character directly to the output. This particular code isn't exactly performance-critical but it's useful to acquire good coding habits from the beginning.

Use C idioms

It's more idiomatic C to have a loop like this for the characters:

while(*pArr) { /* ... */ }

rather than this:

while(*pArr != '\0') { /* ... */ }

Consider using a "sentinel" value for the end of a list

There are two common ways to iterate through a list in C. One is as you already have it, when you know how many values are in the list. The other way is to use a "sentinel" value -- some unique value that tells the program "this is the end of the list." For a list of names such as this program has, a rational choice for a sentinel value would be NULL.

Omit the return 0 from the end of main

Uniquely for main, if the program gets to the end of the function, it automatically does the equivalent of return 0 at the end, so you can (and should) omit it.

Result

Using all of these suggestions, one possible rewrite might look like this:

#include <stdio.h>

int main() 
{
    const char *names[] = { "John", "Mona", "Lisa", "Frank", NULL };

    for (int i=0; names[i]; ++i) {
        const char *ch = names[i]; 
        while(*ch) {
            putchar(*ch++);
            putchar('\n');
        }
        putchar('\n');
    }
} 
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 1314658 โ€บ why-all-the-characters-of-an-array-are-not-printed
Why all the characters of an array are not printed? - Microsoft Q&A
June 26, 2023 - Using the %s type specifier with printf for an array that is not null terminated is an error. You may get lucky or the contents of the a array will be printed and followed by garbage characters (whatever is in memory until a null is encountered) or experience other undefined behavior.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ how-to-print-an-array-in-c
How to Print an Array in C
February 18, 2025 - Since character arrays (strings) in C end with a null terminator '\0', we can print them directly using printf("%s", name). ... Using a for loop: The most common and efficient way to print each element of an array.