When printf() is called with %s it will more or less do the following

for(i = 0; pointer[i] != '\0'; i++)
{
   printf("%c", pointer[i]);
}

What you want do is something like

for(i = 0; pointer[i] != '\0'; i++)
{
   printf("\n Element is %c", pointer[i]);
}

When you are referencing specific elements in an array you must use a pointer AND an index i.e. pointer[0] in this case 0 is the index and 'pointer' is the pointer. The above for loops will move through the entire array one index at a time because 'i' is the index and 'i' increases at the end of every rotation of loop and the loop will continue to rotate until it reaches the terminating NULL character in the array.

So you might want to try something along the lines of this.

void printoutarray(char *pointertoarray)
{
  for(i = 0; i <= 2; i++)
  {
    printf("\n Element is: %s \n", pointertoarray[i]);
  }
}
Answer from John Vulconshinz on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ printing string array seperately in c
r/C_Programming on Reddit: printing string array seperately in C
April 13, 2021 -

Hi,

I have a string like this, which I'd be entering in the standard input. (Not as arguments, once the program has started, then.)

The word 'Message' is fixed input here .But 'George' is the name and can be replaced with any name (of any length). Also the the message; 'Good Evening' is also variable. User can input any message to stdin.

What the program should do is that, it should print this is return as output:

The above shown is the output format.

I have tried it like this but it doesnt seem to come out right:

char buff[20]; // This has the stdin

for(int i = 7; i <strlen(buff); ++i){ // i=7 because I'm trying to skip 'Message' fprintf(stderr, "(%s)", &buff[i]);

}

I know that the above code doesn't consider all the factors. (I'm confused as how to separate each factor!)

I don't understand how I can consider till the name to be one print and then from the name to the end of the message as another (removing the colon in between) and printing all this together!

Any help would be appreciated.

Discussions

C Print out a string array - Stack Overflow
I'm very new to programming in C and I'm trying to print an array of strings. I can enter the input easily enough but then when it tries to print the array the program stops responding. Any help is More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 26, 2017
c - Printing the contents of a string array using pointers - Code Review Stack Exchange
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm currently studying C and I'm trying to just print the contents of a string array. More on codereview.stackexchange.com
๐ŸŒ codereview.stackexchange.com
December 24, 2015
Help in printing 2 d string array in c
I had trouble removing the last name because it prints twice. I just want it to print once. One print for every name of the array. Sample output: Here is the code: #include void print(char (*names)[3][5], int outer, int inner); int main() { char name[][3][5] = {{"Dab", "Gel", "Rish"}, {"Ron", ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
1
0
November 30, 2021
Calling Julia from C: printing array of strings?
Iโ€™m aware that there are other, perhaps better, ways of doing this - but Iโ€™m trying to broaden my understanding of how the various pieces work. I have a C program that contains an array of strings, and Iโ€™d like to printโ€ฆ More on discourse.julialang.org
๐ŸŒ discourse.julialang.org
10
0
June 22, 2023
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ create-and-print-array-of-strings.aspx
C program to create and print array of strings
We have posted programs on strings ... 50 int main() { //declaration char strings[MAX_STRINGS][STRING_LENGTH]; int loop, n; printf("Enter total number of strings: "); scanf("%d", &n); printf("Enter strings...\n"); for (loop = 0; loop < n; loop++) { printf("String [%d] ...
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-strings
Strings in C (With Examples)
Learn more about passing arrays to a function. #include <stdio.h> void displayString(char str[]); int main() { char str[50]; printf("Enter string: "); fgets(str, sizeof(str), stdin); displayString(str); // Passing string to a function.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to print string in c
How to Print String in C - Scaler Topics
April 16, 2024 - In the sections below, let's look at the methods used to print a string. puts(<str_name>): puts() function takes a string character array name as an argument to print the string in the console.
Find elsewhere
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');
    }
} 
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ print char array in c
How to Print Char Array in C | Delft Stack
February 2, 2024 - This flexibility can be useful when you need to extract substrings or print a portion of the character array. While the printf function is a versatile tool for formatted output, the puts function offers a straightforward way to print strings and character arrays in C.
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ array-of-strings-in-c
Array of Strings in C - C Programming Tutorial - OverIQ.com
The ch_arr is a pointer to an array of 10 characters or int(*)[10]. Therefore, if ch_arr points to address 1000 then ch_arr + 1 will point to address 1010. ... ch_arr + 0 points to the 0th string or 0th 1-D array. ch_arr + 1 points to the 1st string or 1st 1-D array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_array_of_strings.htm
Array of Strings in C
To print each string of an array of strings, you can use the for loop till the number of strings.
๐ŸŒ
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
printf(โ€œs\nโ€, a) also puts the array out. Are you really sure you should be using anything as complicated as a computer? ... RelatedHow do you go about writing a code that prints a string made up of the first letter of each word in a given ...
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c โ€บ program โ€บ print-string
C program to print a string | Programming Simplified
The printf function prints the argument passed to it (a string). Next, we will see how to print it if it's stored in a character array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ learn_c_by_examples โ€บ program_to_print_array_in_c.htm
Program to print array in C
START Step 1 โ†’ Take an array A and define its values Step 2 โ†’ Loop for each value of A Step 3 โ†’ Display A[n] where n is the value of current iteration STOP ยท Let's now see the pseudocode of this algorithm โˆ’ ยท procedure print_array(A) FOR EACH value in A DO DISPLAY A[n] END FOR end procedure
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ how-to-print-a-string-array-in-c
How to Print a String Array in C
February 19, 2025 - In this example, we use a while loop to print all the strings from an array. ... #include <stdio.h> int main() { // Declare an array of strings char *colors[] = {"Red", "Green", "Blue", "Yellow", "Purple"}; int size = sizeof(colors) / sizeof(colors[0]); int i = 0; // Print each string using a while loop while (i < size) { printf("%s\n", colors[i]); i++; } return 0; }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-strings-in-c
Array of Strings in C - GeeksforGeeks
July 23, 2025 - In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings).
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 109563-how-do-i-print-out-two-dimensional-array-strings.html
How do I print out a two dimensional array of strings?
November 23, 2008 - &#37;c is used to print a single ... So in that sense, a 2D-array of characters is an array of strings. Printf wasn't designed to print an array of strings, so you have to feed it with individual strings....
Top answer
1 of 5
1

The straight forward approach is simply:

#include <ansi_c.h>//collection of all ansi C headers files in my environment.  
                   //replace this with appropriate headers supporting your environment
int main()
#define MAX_DIGIT_LEN 10 //memory allocation for char *b

{
   int a[5] ={1,2,3,4,5};
   int i ; 
   char *b; //in your original post, you never set 
            //this pointer to point to any space
            //it was therefore an uninitialized 
            //pointer, with no memory space.  

   //you must initialize pointers, and allocate some memory.
   //Somthing like this will work:   

   //memory must be MAX_DIGIT_LEN * number of ints in array 
   b = malloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1);//using "sizeof(a)/sizeof(a[0]" 
                                        //will accommodate array size changes 
                                        //+1 allows for NULL byte at end of char array
   sprintf(b, "%d%d%d%d%d", a[0],a[1],a[2],a[3],a[4]);    
   printf("\n %s",b);
   free(b);

   return 0;
}  

Produces this output:

If you want to use a loop, then consider using (creating) an additional variable:

#include <ansi_c.h>//collection of all ansi C headers files in my environment.  
                   //replace this with appropriate headers supporting your environment

#define MAX_DIGIT_LEN 10 //memory allocation for char *b
int main()  
{
   int a[5] ={1,2,3,4,5};
   int i ; 
   char *b;
   char buf[MAX_DIGIT_LEN];
               //create this as an intermediary place 
               //to store discrete elements of a[]
               //arbitrarily chose 10, to hold up to 
               //9 digit integer, such as 123456789.
               //if you anticipate larger digits, 
               //use a bigger index to create buf[] 

   //memory must be MAX_DIGIT_LEN * number of ints in array 
   b = calloc(MAX_DIGIT_LEN*(sizeof(a)/sizeof(a[0]))+1, 1);
                                           //note calloc (rather than malloc) is 
                                           //preferable here to initialize
                                           //all memory space with 0 before
                                           //attempting strcat() calls.

   for(i=0 ; i<sizeof(a)/sizeof(a[0]) ; i++)
   {   
       sprintf(buf, "%d", a[i]);
       strcat(b,buf);
   }      
   printf("\n %s",b);
   free(b);

   return 0;
}  

Produces this output:

2 of 5
0

You need to correct your for loop (moving with the pointer)

for(i = 0 ; i < 5 ; ++i)
{   
   sprintf(&b[i], "%d", a[i]);
}