char name[30];
One-dimensional character array named name. Size = 30 elements. Each element holds one character. Storage class = automatic (defined inside a function). Can store a string up to 29 characters + null terminator '\0'.
char name[30]; /* 30-element char array, auto storage */ /* stores strings up to 29 chars + '\0' */ scanf("%s", name); /* read a name */ printf("%s", name); /* print it */
float c[6];
One-dimensional float array named c. Size = 6 elements. Each element is a floating-point number. Storage class = automatic. Indices run from c[0] to c[5]. Not initialised โ contains garbage values.
#define N 50 โ static int a[N];
#define N 50 creates a symbolic constant. static int a[N] is a static one-dimensional integer array of 50 elements. Static means it is initialised to 0 automatically and retains its value between function calls.
#define N 50 static int a[N]; /* 50-element static int array */ /* all 50 elements auto-init to 0 */ /* persists between function calls */
int a[N]; int params[5][5];
int a[N] โ one-dimensional int array of N elements (N from #define).
int params[5][5] โ two-dimensional integer array, 5 rows ร 5 columns = 25 elements total. Stored in row-major order in memory.
#define A 88 ยท #define B 155
Two symbolic constants โ A = 88 and B = 155. Can be used as array sizes: int table[A][B] creates an 88ร155 = 13,640 element 2D integer array. The constants make changing the size easy.
char sens[4][10]; double accounts[50][25][80];
char sens[4][10] โ 2D char array, 4 rows ร 10 cols. Stores 4 strings of up to 9 characters each.
double accounts[50][25][80] โ three-dimensional double array. Total elements = 50 ร 25 ร 80 = 100,000 doubles. Each double = 8 bytes โ total memory = 800,000 bytes = ~781 KB!
float c[8] = {0., 5., 3., -4., 12., 12., 0., 8.};
8-element float array. Values assigned in order: c[0]=0.0, c[1]=5.0, c[2]=3.0, c[3]=-4.0, c[4]=12.0, c[5]=12.0, c[6]=0.0, c[7]=8.0. All 8 elements specified so none default to 0.
float c[8] = {2., 0., 5., 0., 3., -4.};
8-element float array, only 6 values given. First 6 elements: c[0]=2.0, c[1]=0.0, c[2]=5.0, c[3]=0.0, c[4]=3.0, c[5]=-4.0. Remaining 2 elements automatically set to 0: c[6]=0.0, c[7]=0.0.
float c[8] = {2., 0., 5., 0., 3., -4.}; /* c[0]=2.0 c[1]=0.0 c[2]=5.0 c[3]=0.0 c[4]=3.0 c[5]=-4.0 c[6]=0.0 c[7]=0.0 (auto) */
int s[12] = {0, 0, 8, 0, 0, 6};
12-element int array, only 6 values given. s[0]=0, s[1]=0, s[2]=8, s[3]=0, s[4]=0, s[5]=6. Remaining 6 elements s[6] through s[11] = 0 automatically.
char flag[4] = {'T', 'R', 'U', 'E'};
4-element character array. Values: flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E'. Note: This is NOT a null-terminated string because there is no '\0' at the end. It is just 4 characters. To make it a string, declare char flag[5].
char flag[5] = {'T', 'R', 'U', 'E'};
5-element char array, 4 values given. flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E', flag[4]='\0' (auto zero = null terminator). This IS a valid null-terminated string "TRUE".
char flag[] = "TRUE";
Auto-sized char array initialised with string literal. Compiler sets size = 5 (4 chars + '\0'). Values: flag[0]='T', flag[1]='R', flag[2]='U', flag[3]='E', flag[4]='\0'. This is the standard way to declare a string.
char flag[] = "TRUE"; /* size=5, auto-adds '\0' */ char flag[] = "FALSE"; /* size=6 */ printf("%s\n", flag); /* prints: FALSE */
int p[2][4] = {1, 3, 5, 7};
2ร4 = 8 element integer array, only 4 values given. Filled row by row: p[0][0]=1, p[0][1]=3, p[0][2]=5, p[0][3]=7. Remaining 4 elements of row 1 all become 0: p[1][0]=p[1][1]=p[1][2]=p[1][3]=0.
int p[2][4] = {1, 1, 3, 3, 5, 5, 7, 7};
All 8 elements specified. Filled row by row: Row 0: p[0][0]=1, p[0][1]=1, p[0][2]=3, p[0][3]=3. Row 1: p[1][0]=5, p[1][1]=5, p[1][2]=7, p[1][3]=7.
int p[2][4] = {{1,3,5,7},{2,4,6,8}};
All 8 elements with row braces. Row 0: p[0] = {1,3,5,7}. Row 1: p[1] = {2,4,6,8}. Cleanest and most readable format for 2D array initialisation.
/* (i) Both rows fully specified with inner braces */ int p[2][4] = {{1,3,5,7},{2,4,6,8}}; /* (k) Only 2 values in row 0, only 2 in row 1 */ int p[2][4] = { {1, 3}, /* p[0][0]=1, p[0][1]=3, p[0][2]=0, p[0][3]=0 */ {5, 7} /* p[1][0]=5, p[1][1]=7, p[1][2]=0, p[1][3]=0 */ };
int c[2][3][4] = {{{1,2,3},{4,5},{6,7,8,9}},{{10,11},{},{12,13,14}}};
3D array โ 2 layers, 3 rows each, 4 columns each. Total = 24 elements. Filled layer by layer:
- Layer 0, Row 0: {1,2,3,0} โ 3 given, 4th defaults to 0
- Layer 0, Row 1: {4,5,0,0} โ 2 given, rest 0
- Layer 0, Row 2: {6,7,8,9} โ all 4 given
- Layer 1, Row 0: {10,11,0,0} โ 2 given
- Layer 1, Row 1: {0,0,0,0} โ empty braces, all 0
- Layer 1, Row 2: {12,13,14,0} โ 3 given
int c[2][3][4] = { { /* Layer 0 */ {1, 2, 3}, /* row 0: {1,2,3,0} */ {4, 5}, /* row 1: {4,5,0,0} */ {6, 7, 8, 9} /* row 2: {6,7,8,9} */ }, { /* Layer 1 */ {10, 11}, /* row 0: {10,11,0,0} */ {}, /* row 1: {0,0,0,0} */ {12, 13, 14} /* row 2: {12,13,14,0} */ } };
char colors[3][6] = {{'R','E','D'},{'G','R','E','E','N'},{'B','L','U','E'}};
2D char array โ 3 rows ร 6 cols. Each row stores one colour name (as chars, NOT strings unless null-terminated):
- Row 0:
'R','E','D','\0','\0','\0'โ "RED" + 3 auto zeros (= null terminators) - Row 1:
'G','R','E','E','N','\0'โ "GREEN" + 1 auto zero - Row 2:
'B','L','U','E','\0','\0'โ "BLUE" + 2 auto zeros
Because unused positions become 0 (= '\0'), each row is a valid null-terminated string!
char colors[3][6] = { {'R','E','D'}, {'G','R','E','E','N'}, {'B','L','U','E'} }; /* Each row IS a null-terminated string because */ /* unused positions auto-fill with 0 = '\0' */ printf("%s\n", colors[0]); /* prints RED */ printf("%s\n", colors[1]); /* prints GREEN */ printf("%s\n", colors[2]); /* prints BLUE */
12-element int array called c โ values 1, 4, 7, 10, ..., 34
Pattern: starts at 1, increases by 3 each time. Values: 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34. That is 12 values (check: 1 + 11ร3 = 34 โ).
#include <stdio.h> int main() { /* Method 1: Direct initialisation */ int c[12] = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34}; /* Method 2: Using formula c[i] = 1 + i*3 */ int c2[12], i; for(i=0; i<12; i++) c2[i] = 1 + i*3; printf("c: "); for(i=0; i<12; i++) printf("%d ", c[i]); printf("\n"); return 0; }
c: 1 4 7 10 13 16 19 22 25 28 31 34
1D char array called point โ string "NORTH" with null
char point[] = "NORTH"; /* Best: auto-adds '\0' */ /* size = 6 automatically */ /* Alternative: explicit chars with '\0' */ char point2[6] = {'N','O','R','T','H','\0'}; printf("%s\n", point); /* NORTH */
4-element char array called letters โ 'S', 'E', 'W' (and more?)
Four-element array, 3 characters given. The 4th element defaults to '\0' (null) automatically. So this stores the string "SEW".
char letters[4] = {'S', 'E', 'W'}; /* letters[0]='S' letters[1]='E' letters[2]='W' letters[3]='\0' */ printf("%s\n", letters); /* SEW */
6-element float array called consts โ 0.005, -0.032, 1e-6, 0.167, -0.3e8, 0.015
Six float values including scientific notation. 1e-6 = 0.000001 and -0.3e8 = -30,000,000. These are valid C float literals.
#include <stdio.h> int main() { float consts[6] = { 0.005, /* consts[0] */ -0.032, /* consts[1] */ 1e-6, /* consts[2] = 0.000001 */ 0.167, /* consts[3] */ -0.3e8, /* consts[4] = -30000000.0 */ 0.015 /* consts[5] */ }; for(int i=0; i<6; i++) printf("consts[%d] = %g\n", i, consts[i]); return 0; }
consts[0] = 0.005 consts[1] = -0.032 consts[2] = 1e-06 consts[3] = 0.167 consts[4] = -3e+07 consts[5] = 0.015
1e-6 prints as 1e-06 and -0.3e8 prints as -3e+07.Checklist
- I can describe any array declaration โ type, dimensions, size, storage class
- Partial initialisation: missing elements automatically become 0
- char flag[] = "TRUE" auto-adds '\0' and sets size=5
- static arrays auto-initialise all elements to 0
- 2D array partial row braces: missing elements in that row โ 0
- 3D array: empty braces {} set all elements of that sub-array to 0
- Problem 9.29a: formula c[i] = 1 + i*3 generates the sequence
- Problem 9.29d: scientific notation 1e-6 and -0.3e8 are valid float literals