Programming in C: A Small Note on Arrays

Arrays in C are collections of elements of the same data type, stored in contiguous memory locations. They are indexed starting from 0, and the subscript (index) used to access an array element can be an expression that evaluates to an integer.

Accessing Arrays and Bounds

It is important to note that accessing an array outside its declared bounds does not necessarily produce an error, but it leads to undefined behavior. This means that the program may read or write unintended memory locations, potentially causing crashes or unexpected results.

Example of an Array Declaration

If an array is declared as:

int array[10] = {10};

  • The first element (array[0]) is initialized to 10.
  • All remaining elements (array[1] to array[9]) are automatically initialized to 0.

Incorrect Declaration Example

A common mistake in character array initialization:

char alpha[3] = {a, b, c};  // Incorrect

Here, a, b, and c are not enclosed in single quotes, so the compiler will not recognize them as character literals.

Correct Declaration Example

To correctly initialize a character array, use single quotes for characters:

char alpha[3] = {'a', 'b', 'c'};

Alternatively, a string (null-terminated character array) can be declared as:

char alpha[] = "abc";  // Automatically allocates space for 'a', 'b', 'c', and '\0'

Array Indexing with Expressions

C allows the use of expressions as array indices. For example:

int numbers[5] = {10, 20, 30, 40, 50};
int index = 2;
printf("%d", numbers[index + 1]);  // Output: 40

This flexibility allows dynamic indexing in programs.

Avoiding Out-of-Bounds Access

To prevent accessing elements outside the valid range, always ensure that indices are within the defined size of the array:

int arr[5] = {1, 2, 3, 4, 5};
int idx = 6;  // Out of bounds

if (idx >= 0 && idx < 5) {
    printf("%d", arr[idx]);
} else {
    printf("Index out of bounds!\n");
}

Summary

  • Arrays in C have zero-based indexing.
  • Accessing an index outside the declared range results in undefined behavior.
  • Partial initialization of an array fills the remaining elements with zeros (for static or global arrays).
  • Character arrays should use single quotes for characters ('a', 'b') and double quotes for strings ("abc").
  • Always validate array indices to prevent unintended memory access.

Understanding these fundamentals helps in writing safe and efficient C programs.

Programming in C: Understanding Expression Values

In C, expressions that involve comparison operators, such as (n == 0), (x != n + 1), or getchar() != EOF, evaluate to either 0 (false) or 1 (true). This follows the convention that any nonzero value is considered true, while zero represents false.

End of File (EOF) and Its Value

The symbolic constant EOF (End of File) is used to indicate the end of input when reading from a stream, commonly used with functions like getchar(), fgetc(), and scanf().

Although EOF is typically defined as -1, its exact value is implementation-dependent and may vary between different compilers or platforms. It is always defined in <stdio.h> and should be used as EOF rather than relying on its numeric value.

Character Constants and Their Integer Values

Character constants in C, such as 'A', ' ', and '0', are stored as integer values based on their ASCII codes. For example:

  • 'A' has an ASCII value of 65
  • ' ' (newline) has an ASCII value of 10
  • '0' has an ASCII value of 48

This allows characters to be used in arithmetic operations, such as calculating numeric values from character digits:

char digit = '5';
int number = digit - '0'; // Converts '5' to integer 5

Additional Notes on Expression Values

  • Logical expressions using &&, ||, and ! also return 0 or 1.
  • Bitwise operations (such as &, |, and ^) operate at the binary level and return results based on bitwise evaluation rather than 0 or 1.
  • Conditional expressions like n ? x : y return x if n is nonzero (true) and y if n is zero (false).

Understanding how expressions evaluate in C is fundamental for writing efficient and bug-free code, especially when dealing with conditional logic and input handling.