Example:
#include <stdio.h>
#define ROWS 2
#define COLS 3
/*
* Program to show various array capabilities
*/
int main ( void ) {
/* define a static array */
int days [] = { 1, 2, 3, 4, 5 };
/* define a static array of characters (aka a string) */
char string [] = "this is a string";
/* two dimensional array using definitions from above */
float numbers [ROWS][COLS];
int row, col;
/* remember - the sizeof operator includes the terminator */
printf ( "Length of the string (including the terminator): %d\n",
sizeof ( string ) );
/* loop over the array printing all values */
for ( row = 0; row < ROWS; row++ ) {
for ( col = 0; col < COLS; col++ ) {
numbers [row][col] = (float) row * col;
printf ( "numbers [%d][%d] = %5.2f\n", row, col, numbers [row][col] );
}
}
return ( 0 );
}
Output:
Length of the string (including the terminator): 17
numbers [0][0] = 0.00
numbers [0][1] = 0.00
numbers [0][2] = 0.00
numbers [1][0] = 0.00
numbers [1][1] = 1.00
numbers [1][2] = 2.00