#include void print_array(int *arr, size_t len) { putchar('{'); int i; for (i = 0; i < len - 1; i++) printf("%d, ", arr[i]); printf("%d}\n", arr[i]); } int main(void) { int arr[] = {1, 2, 3}; size_t len = 3; print_array(arr, len); return 0; } Answer from 0Naught0 on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to print a full array?
r/C_Programming on Reddit: How to print a full array?
September 7, 2024 -

In python, I would use a list to store some numbers than print all of them:
x = [1, 2, 3, 4]

print(x) #output = [1, 2, 3, 4]

How should I do it in C with an array?

Another question: is an array similar to the python lists? If not, what type would be it?;

Top answer
1 of 6
44

What you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.

for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
        printf("%d ", array[i][j]);
    }
    printf("\n");
} 

This will print it in the following format

10 23 42
1 654 0
40652 22 0

if you want more exact formatting you'll have to change how the printf is formatted.

2 of 6
13

There is no .length property in C. The .length property can only be applied to arrays in object oriented programming (OOP) languages. The .length property is inherited from the object class; the class all other classes & objects inherit from in an OOP language. Also, one would use .length-1 to return the number of the last index in an array; using just the .length will return the total length of the array.

I would suggest something like this:

int index;
int jdex;
for( index = 0; index < (sizeof( my_array ) / sizeof( my_array[0] )); index++){
   for( jdex = 0; jdex < (sizeof( my_array ) / sizeof( my_array[0] )); jdex++){
        printf( "%d", my_array[index][jdex] );
        printf( "\n" );
   }
}

The line (sizeof( my_array ) / sizeof( my_array[0] )) will give you the size of the array in question. The sizeof property will return the length in bytes, so one must divide the total size of the array in bytes by how many bytes make up each element, each element takes up 4 bytes because each element is of type int, respectively. The array is of total size 16 bytes and each element is of 4 bytes so 16/4 yields 4 the total number of elements in your array because indexing starts at 0 and not 1.

Discussions

Print an array of objects in c - Stack Overflow
I was hoping someone could tell me how print out an array with objects in c. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Print Out an Array of Objects C++ - Stack Overflow
I'm trying to figure out how to print out the values of the above array of objects using the class parmeterized constructor (it's supposed to print out the radius of each Circle object). More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I print array of objects? Objective C - Stack Overflow
I have the class Stockholging where its created the properties and methods costInDollars & valueInDollars.. I need to create a instance of each stock add each object to the array and then print... More on stackoverflow.com
๐ŸŒ stackoverflow.com
C: print an array - Stack Overflow
I've started learning C yesterday, and the only other language I know is Python. I'm having some trouble with arrays, because they are quite different from the Python lists. I tried to print an a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ learn_c_by_examples โ€บ program_to_print_array_in_c.htm
Program to print array in C
This program will let you understand that how to print an array in C. We need to declare & define one array and then loop upto the length of array. At each iteration we shall print one index value of array.
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ array โ€บ c-array-exercise-1.php
C Program: Read and Print elements of an array - w3resource
September 27, 2025 - The above program first prompts ... elements of the array using another for loop that runs from i=0 to i<n and uses the printf() function to print each element of the array....
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-print-out-an-array-in-C
How to print out an array in C - Quora
Answer (1 of 8): This is a simple program to create an array and then to print it's all elements. Now, just know about arrays. Arrays are the special variables that store multiple values under the same name in the contiguous memory allocation. Elements of the array can be accessed through their...
Find elsewhere
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ how to print an array in c
How to Print an Array in C - Know Program
March 29, 2021 - How to Print an Array in C | To print an array we need to use loops. The loop can be either for loop, while loop, or do-while loop.
Top answer
1 of 1
1

You need to overload the <<operator for the Circle class as you need it (here you need to display the radius of the Circle object) As a parameter, you give it a Circle object.

In the class, I added the line :

friend std::ostream& operator<< (std::ostream &out, const Circle &matrix);

And outside the class its definition like this:

std::ostream& operator<< (std::ostream& out, const Circle& matrix)
{
    out << "Circle radius " << matrix.radius<<std::endl;
}

Then in the main function, your loop shall be modified to only give a Circle object like this:

for (int index = 0; index < size; index++)
{
      //cout << arr[index].Circle();  //Error: Invalid use of Circle::Circle
      cout << arr[index]; // no error as you defined operator <<for your Circle class
}

Here is you complete code modified

#include <iostream>

using namespace std;

class Circle
{
    private:
        double radius;
    public:
        Circle() //Constructor
        {   radius = 0; }

        Circle(double); //Parameterized Constructor

        void setRadius(double);

        double getRadius() const
        { return radius; }

        double getArea() const
        { return 3.14 * radius * radius; }

        friend std::ostream& operator<< (std::ostream &out, const Circle &matrix);

};

std::ostream& operator<< (std::ostream& out, const Circle& matrix)
{
    out << "Circle radius " << matrix.radius<<std::endl;
}

Circle::Circle(double r)
{
    radius = r;
}

void Circle::setRadius(double r)
{
    radius = r;
}

int main()
{
    int size = 3;  //Set size of array of objects
    Circle arr[size] = { Circle(1.2), Circle(2.2), Circle(5.5) }; //Array of Objects

    for (int index = 0; index < size; index++)
    {
      //cout << arr[index].Circle();  //Error: Invalid use of Circle::Circle
      cout << arr[index]; // no error as you defined operator <<for your Circle class    
   }

    Circle b(2.8);                  //Instance of class
    cout << b.getRadius();          //Prints out the radius of object

    Circle c;                       //Another instance
    cout << endl << c.getRadius(); //Another radius

    double r;                     //Variable to store the radius based on user input
    cout << "\nEnter a radius: ";
    cin >> r;
    c.setRadius(r);             //Pass user input into radius member function
    cout << c.getRadius();      //Print new radius of object c
    cout << endl << c.getArea(); //Prints area of object c.


    return 0;

}

The output is:

Circle radius 1.2
Circle radius 2.2
Circle radius 5.5
2.8
0
Enter a radius: 5
5
78.5
๐ŸŒ
Medium
medium.com โ€บ @ryan_forrester_ โ€บ print-array-in-c-a-comprehensive-guide-4a2171b122e3
Print Array in C++: A Comprehensive Guide | by ryan | Medium
October 17, 2024 - This example demonstrates how to print an array (or vector) of custom objects by defining a custom output operator.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-to-print-elements-in-an-array
C Program to Print Elements in an Array
April 5, 2025 - In this article, we will show How to write a C Program to Print Elements in an Array using For Loop, While Loop, and Functions with examples.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 82860-using-printf-print-out-array.html
Using printf to print out an array
September 11, 2006 - I'm not use to C...I'm just trying to find a bug in someone elses C code that happens to affect my other code. Thanks (Stats is an int array) Last edited by slowcoder; 09-11-2006 at 02:05 PM. ... int i; for (i=0; i<4; i++) { printf("%d %d\n", Stats[i][0], Stats[i][1]); } Or you you want hex, then:
๐ŸŒ
Academic Help
academichelp.net โ€บ coding โ€บ c-coding โ€บ how-to-print-an-array.html
How to Print an Array in C: Explanation&Examples
October 31, 2023 - The C programming language provides a versatile platform for various applications, from operating systems to simple algorithms. One of the foundational aspects of C programming is the management and utilization of arrays. In this article, we will delve deep into how to print an array in C using various methods, including for loops, while loops, and even recursion.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-arrays
Arrays in C - GeeksforGeeks
To know how to pass an array to a function in C, refer to this article โ€” Arrays and Pointers ยท For working with tables, matrices, or grids, C allows the use of arrays with more than one dimension.
Published ย  October 17, 2025
๐ŸŒ
OneCompiler
onecompiler.com โ€บ c โ€บ 3wzzbmde5
Print elements array - C - OneCompiler
#include <stdio.h> int main() { char name[50]; printf("Enter name:"); scanf("%s", name); printf("Hello %s \n" , name ); return 0; } C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 59606
How to display Int Arrays using Printf? - C++ Forum
January 17, 2012 - I would like to print out all the values present in the Numbers array in a line, so the output display should be: 153 I have got it working with the normal way: printf("%d %d %d, Numbers[0], Numbers[1], Numbers[3] I was wondering is there an easy way to display these arrays without having to do %d for each of the array? Thanks! ... Hey jzp253 Ermm yeah I could but then the only data that will be displayed will be the on in the 10th array of the Numbers variable?
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ array printing function in c
r/cs50 on Reddit: Array printing function in C
November 28, 2020 -

A C function for printing the entire array, feel free to use it!

void printArray(int length, int array[])

{

for (int i = 0, n = length; i < n; i++)

{

if (i == n - 1)

{

printf("%i.", array[i]);

}

else

{

printf("%i, ", array[i]);

}

}

printf("\n");

}