A float array a[10][20] (4 bytes per element) starts at address 2000. Find the address of a[5][8] (a) with 0-based C indexing; (b) if the bounds are a[1..10][1..20], stored row-major; (c) with the same bounds stored column-major.
An array stores many values of one type in consecutive memory, indexed from 0; a string is a char array ended by '\0'. Address calculation, initialisation rules, decay to pointers and the string.h functions are the most-asked parts.
int a[5] holds a[0]…a[4] in consecutive memory: 5 × sizeof(int) bytes. C does no bounds checking — a[5] is undefined behaviour.int a[5] = {1, 2}; gives 1 2 0 0 0, and int a[] = {1, 2, 3}; gets size 3.char s[] = "hi" is a modifiable copy; char *p = "hi" points to a read-only literal, and writing through p is undefined.An array is a fixed-size collection of elements of the same type, stored in consecutive memory locations and reached through an index (subscript). int marks[5]; reserves room for five ints named marks[0] to marks[4]. Indexing always starts at 0, so the last valid index is size − 1. One array name stands for a whole table of values that a loop can process.
#include <stdio.h>
int main(void)
{
int marks[5]; /* 5 ints: marks[0] .. marks[4] */
int i, sum = 0;
for (i = 0; i < 5; i++)
marks[i] = (i + 1) * 10; /* 10 20 30 40 50 */
for (i = 0; i < 5; i++)
sum += marks[i];
printf("sum = %d, average = %.1f\n", sum, sum / 5.0); /* sum = 150, average = 30.0 */
printf("bytes = %zu, elements = %zu\n",
sizeof marks, sizeof marks / sizeof marks[0]); /* bytes = 20 (4-byte int), elements = 5 */
return 0;
}int a[N]; with #define N 10). C99 added variable-length arrays (int a[n]; with n known only at run time, local arrays only); C11 made them optional.a[5] or a[-1] on a 5-element array compiles and is undefined behaviour — it may read garbage, silently corrupt another variable or crash.a = b; is an error and a == b compares addresses. Copy element by element or with memcpy.a and &a[0] have the same value.double arr[50]; allocates 50 × 8 = 400 bytes (a KPSC question). For int arrays, check the assumed size: int a[50] is 100 bytes with 16-bit Turbo C (2-byte int) and 200 bytes with GCC (4-byte int).| Declaration | Contents | Rule |
|---|---|---|
int a[5] = {1, 2, 3, 4, 5}; | 1 2 3 4 5 | one value per element |
int a[5] = {1, 2}; | 1 2 0 0 0 | missing initialisers become 0 |
int a[5] = {0}; | 0 0 0 0 0 | the idiom for an all-zero array |
int a[] = {4, 8, 15}; | 4 8 15 | size deduced from the list (3) |
int a[3] = {1, 2, 3, 4}; | — | error: too many initialisers |
int a[5]; inside a function | garbage | auto arrays are not initialised |
static int a[5]; or a global array | 0 0 0 0 0 | static storage is zero-filled |
int a[6] = {[4] = 9, [1] = 3}; (C99) | 0 3 0 0 9 0 | designated initialisers |
char s[] = "abc"; | 'a' 'b' 'c' '\0' | the literal is copied in; size 4 |
An initialiser list may be shorter than the array but never longer. Once any element is initialised, every element you did not mention is set to zero — so int count[26] = {0}; is the standard way to start a table of counters. With no initialiser at all, a local (auto) array holds garbage, while a global or static array starts as all zeros.
int a[5] = {1}; does not fill the array with 1s — it gives 1 0 0 0 0. Only zero seems to 'fill everything', because the unmentioned elements are zero anyway.A 2-D array is an array of arrays: int m[3][4]; has 3 rows, each an array of 4 ints — 12 ints in all — and m[i][j] is the element in row i, column j. Initialise row by row with nested braces, int m[2][3] = {{1, 2, 3}, {4, 5, 6}};, or with a flat list {1, 2, 3, 4, 5, 6} that fills row 0 first. Only the first dimension may be left out: int m[][3] = {…} is fine, int m[2][] = {…} is not.
Memory is one-dimensional, so the rows must be laid end to end. C uses row-major order: all of row 0, then all of row 1, and so on. Fortran and MATLAB use column-major order (column 0 first). Either way, the position of any element follows from a formula — the most common array numerical in exams.
| Element | Elements stored before it | Address |
|---|---|---|
m[0][0] | 0 | 1000 |
m[0][3] | 3 | 1012 |
m[1][0] | 4 (all of row 0) | 1016 |
m[1][2] | 1 × 4 + 2 = 6 | 1024 |
m[2][3] | 2 × 4 + 3 = 11 | 1044 |
#include <stdio.h>
int main(void)
{
int m[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
for (int i = 0; i < 3; i++) {
int row_sum = 0;
for (int j = 0; j < 4; j++)
row_sum += m[i][j];
printf("row %d sum = %d\n", i, row_sum); /* 10, 26, 42 */
}
/* m[1][2] is element number 1*4 + 2 = 6 of the flat layout, so it */
/* starts (1*4 + 2) * 4 = 24 bytes in (4-byte int). Measure in bytes: */
/* subtracting int pointers from different rows is undefined. */
printf("%td\n", (char *)&m[1][2] - (char *)m); /* 24 */
return 0;
}In almost every expression an array name is converted — it decays — to a pointer to its first element. When you pass an array to a function, only that address travels; nothing is copied. The parameter forms int a[], int a[10] and int *a are therefore identical: the 10 is ignored, and the function has no idea how long the array is.
#include <stdio.h>
void show_size(int a[10]) /* really: int *a */
{
printf("inside: %zu\n", sizeof a); /* 8 on 64-bit: a pointer */
}
void double_all(int a[], int n) /* the length must travel separately */
{
for (int i = 0; i < n; i++)
a[i] *= 2; /* changes the CALLER's array */
}
int main(void)
{
int v[5] = {1, 2, 3, 4, 5};
printf("outside: %zu\n", sizeof v); /* 20 = 5 × 4 */
show_size(v);
double_all(v, 5);
printf("%d %d\n", v[0], v[4]); /* 2 10 */
return 0;
}For a 2-D array, every dimension except the first must appear in the parameter, because the function needs the row length to compute addresses: void print(int m[][4], int rows), or equivalently int (*m)[4]. There are three exceptions to decay: sizeof a (size of the whole array), &a (address of the whole array, of type int (*)[5]) and a string literal used to initialise a char array.
const int a[].C has no separate string type. A string is a sequence of characters stored in a char array and ended by the null character '\0' (value 0). The literal "KPSC" therefore occupies 5 bytes: K, P, S, C and the terminator. Every string function relies on that terminator to know where the string stops.
| Declaration | What you get | sizeof | strlen |
|---|---|---|---|
char a[] = "KPSC"; | a modifiable array of 5 chars | 5 | 4 |
char b[10] = "KPSC"; | array of 10; the 6 unused bytes are '\0' | 10 | 4 |
char *c = "KPSC"; | a pointer to a read-only string literal | 8 (the pointer itself) | 4 |
char d[4] = "KPSC"; | legal in C, but no room for '\0' — not a string | 4 | undefined (no terminator) |
char e[] = {'K', 'P', 'S', 'C'}; | 4 chars, no terminator | 4 | undefined |
char f[] = "KPSC\0exam"; | 10 bytes with an embedded '\0' | 10 | 4 |
char *p = "hello"; p[0] = 'H'; is undefined behaviour and usually crashes, because literals are stored in read-only memory. char s[] = "hello"; s[0] = 'H'; is fine, since s is your own copy. Similarly, s = "new text"; is an error for an array (use strcpy) but legal for a pointer.Print a string with printf("%s", s) or puts(s), which adds a newline. Read one with scanf("%s", s) — no &, because s already is an address — but scanf stops at the first whitespace and does not check the buffer size unless you give a width, as in scanf("%19s", s). To read a whole line use fgets(s, sizeof s, stdin), which stops after a newline (keeping it) or when the buffer is full.
gets(s) cannot know how big s is, so a long input line overruns the buffer — the very hole exploited by the 1988 Morris worm. It was deprecated in C99 and removed in C11. Use fgets, and strip its newline with s[strcspn(s, "\n")] = '\0';.| Function | What it does | Returns | Example → result |
|---|---|---|---|
strlen(s) | counts the characters before '\0' | size_t | strlen("Kerala") → 6 |
strcpy(d, s) | copies s, including '\0', into d | d | d must be large enough |
strncpy(d, s, n) | copies at most n chars; pads with '\0' if s is shorter | d | adds no '\0' when strlen(s) ≥ n |
strcat(d, s) | appends s to the end of d | d | "Kerala" + "PSC" → "KeralaPSC" |
strncat(d, s, n) | appends at most n chars and always adds '\0' | d | a safer append |
strcmp(a, b) | compares character by character, by code value | < 0, 0 or > 0 | strcmp("abc", "abd") < 0 |
strncmp(a, b, n) | compares at most n characters | < 0, 0 or > 0 | strncmp("Kerala", "Kernel", 3) → 0 |
strchr(s, c) | finds the first occurrence of character c | pointer or NULL | strchr("programming", 'g') → "gramming" |
strrchr(s, c) | finds the last occurrence of c | pointer or NULL | strrchr("programming", 'g') → "g" |
strstr(s, t) | finds the first occurrence of substring t | pointer or NULL | strstr("data structures", "struct") → "structures" |
How strcmp decides. It compares the two strings character by character using their codes, stopping at the first difference or at the terminator. The result is negative if the first string is smaller at that point, 0 if the strings are identical, and positive if it is larger. The standard guarantees only the sign: many libraries return the difference of the first mismatching characters, while others — and compilers that evaluate a constant call themselves — return −1 or 1. So if (strcmp(a, b) == 0) is the correct equality test, and if (strcmp(a, b)) is true when the strings differ.
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[20] = "Kerala";
char t[10] = "";
strcat(s, "PSC"); /* s = "KeralaPSC" */
printf("%s %zu\n", s, strlen(s)); /* KeralaPSC 9 */
strncpy(t, "Hello", 3); /* "Hel"; t was zero-filled */
printf("%s\n", t); /* Hel */
printf("%d\n", strcmp("Merry", "Merry")); /* 0 */
printf("%d\n", strcmp("Merry", "Christmas") > 0); /* 1: 'M' > 'C' */
printf("%d\n", strcmp("Merry", "Merry Boy") < 0); /* 1: '\0' < ' ' */
printf("%s\n", strchr("programming", 'g')); /* gramming */
printf("%s\n", strstr("data structures", "struct")); /* structures */
return 0;
}char s1[] = "Merry"; strcat(s1, "Christmas"); s1 has only 6 bytes, so the call overruns it — undefined behaviour, even though some old exam keys simply print 'MerryChristmas'. Declare the destination large enough, for example char s1[20].strrev, strupr, strlwr and strcmpi come from Turbo C and Microsoft libraries, not from standard C; portable code must write its own. Treat any option that calls strrev a 'standard library function' as a trap.| Function | True (non-zero) when c is… | Example |
|---|---|---|
isalpha(c) | a letter | isalpha('q') → true |
isdigit(c) | a decimal digit 0–9 | isdigit('7') → true |
isalnum(c) | a letter or a digit | isalnum('_') → false |
isspace(c) | space, \t, \n, \v, \f or \r | isspace('\n') → true |
isupper(c) / islower(c) | an upper-case / lower-case letter | isupper('a') → false |
ispunct(c) | printable but not a letter, digit or space | ispunct('!') → true |
toupper(c) / tolower(c) | returns the converted letter; other characters come back unchanged | toupper('q') → 'Q', toupper('5') → '5' |
The classification functions return non-zero for true — not necessarily 1 — so test them as if (isdigit(c)), never == 1. Their argument must be an unsigned char value or EOF; for a plain char that might be negative, write isalpha((unsigned char)c). In ASCII you could test c >= 'a' && c <= 'z' and convert with c - 32, but the ctype functions are clearer and also work with other character sets.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void reverse(char s[]) /* two indices walk toward the middle */
{
int i = 0, j = (int)strlen(s) - 1;
while (i < j) {
char t = s[i]; s[i] = s[j]; s[j] = t;
i++; j--;
}
}
int is_palindrome(const char s[])
{
int i = 0, j = (int)strlen(s) - 1;
while (i < j)
if (s[i++] != s[j--]) return 0;
return 1;
}
size_t my_strlen(const char *s) /* walk to the terminator */
{
const char *p = s;
while (*p) p++;
return (size_t)(p - s);
}
void my_strcpy(char *d, const char *s)
{
while ((*d++ = *s++) != '\0') /* copies the '\0' too, then stops */
;
}
int main(void)
{
char word[] = "kerala", copy[20];
reverse(word);
printf("%s\n", word); /* alarek */
printf("%d %d\n", is_palindrome("malayalam"), is_palindrome("kerala")); /* 1 0 */
const char *text = "Programming in C 2026";
int vowels = 0, digits = 0, spaces = 0;
for (const char *p = text; *p; p++) {
char c = (char)tolower((unsigned char)*p);
if (strchr("aeiou", c)) vowels++;
else if (isdigit((unsigned char)c)) digits++;
else if (c == ' ') spaces++;
}
printf("%d %d %d\n", vowels, digits, spaces); /* 4 4 3 */
my_strcpy(copy, "PSC");
printf("%s %zu\n", copy, my_strlen(copy)); /* PSC 3 */
return 0;
}Notice the recurring patterns. Reversing and palindrome tests use two indices moving toward each other; counting is a single pass that stops at '\0'; and the library functions are just short loops like my_strlen and my_strcpy. The famous one-line copy while ((*d++ = *s++) != '\0'); assigns a character, advances both pointers, and stops right after copying the terminator.
Try the problem first, then reveal the working one step at a time.
A float array a[10][20] (4 bytes per element) starts at address 2000. Find the address of a[5][8] (a) with 0-based C indexing; (b) if the bounds are a[1..10][1..20], stored row-major; (c) with the same bounds stored column-major.
For char s1[] = "Merry";, what do strcmp(s1, "Merry"), strcmp(s1, "Christmas") and strcmp(s1, "Merry Boy") return on a typical implementation, and what is wrong with strcat(s1, "Christmas")?
On a 64-bit system, give sizeof and strlen for char a[] = "exam";, char b[12] = "exam";, char *c = "exam"; and char d[] = "ex\0am";.
What does int a[6] = {4, 7}; int i, s = 0; for (i = 0; i < 6; i++) s += a[i] * (i + 1); printf("%d %d %d", a[1], a[5], s); print, and what would a[6] = 1; do?
An int array x[5][6] begins at address 500. Using 0-based C indexing, find the address of x[3][4] when an int is (a) 2 bytes (16-bit compiler), (b) 4 bytes.
Predict the output: int b[] = {5, 10, 15, 20}; printf("%zu %d %d", sizeof(b) / sizeof(b[0]), b[1] + b[3], b[2] / b[0]);
Predict each result: (a) char s[20] = "Data"; strcat(s, "Base"); printf("%s %zu", s, strlen(s)); (b) char t[8] = ""; strncpy(t, "Structure", 6); printf("%s", t); (c) printf("%d", strncmp("Kerala", "Kernel", 4) < 0); (d) char str[] = "Hello"; str[2] = '\0'; printf("%s %zu", str, sizeof str);
Write int count_vowels(const char *s) that counts vowels in either case, and give its result for "Programming in C".
Explain why char *p = "hello"; p[0] = 'H'; may crash while char q[] = "hello"; q[0] = 'H'; works. Then say which are legal: (a) q = "world"; (b) p = "world"; (c) strcpy(q, "world"); (d) strcpy(q, "wonderful");
int a[5] has no a[5]; out-of-bounds access is undefined behaviour, not a compile error.if (strcmp(a, b)) is true when the strings differ. Comparing strings with == compares addresses.int a[5] = {1}; gives 1 0 0 0 0, not five 1s; only the elements you leave out are zeroed.char s[5] = "hello"; is legal in C (no room for '\0', so s is not a string) but an error in C++; gets() was removed in C11, and strrev/strupr are not standard.10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
double arr[50]; occupy, with 8-byte doubles?Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.
double prices[50]; reserve, given 8-byte doubles?