C Language Key Concepts and Frequent Errors
1. Integer Literal Representations
- Fundamentals: On most modern systems, an
intoccupies 4 bytes (32 bits). The Most Significant Bit (MSB) serves as the sign bit, where 0 indicates a positive value and 1 indicates a negative value. - Base Conversion: To convert a decimal number to binary, repeatedly divide the number by 2. The remainders, read from bottom to top, represent the binary sequence.
- Literals:
- Decimal:
10 - Octal: Starts with
0, e.g.,012 - Hexadecimal: Starts with
0x, e.g.,0xA
- Decimal:
2. Operator Precedence and Behavior
- Priority Order: Logical NOT (
!) > Arithmetic Operators > Relational Operators > Logical AND (&&) / OR (||). Parentheses can override this hierarchy. - Sizeof Operator:
sizeofis a unary operator, not a function call. It returns the memory size in bytes. - Short-Circuit Evaluation:
- For
A || B: IfAevaluates to true,Bis not evaluated. - For
A && B: IfAevaluates to false,Bis not evaluated.
- For
3. Array Characteristics
- Parameter Decay: When passing an array to a function, the parameter receives the address of the first element (pointer). Consequently, applying
sizeof(array_name)inside the function returns the size of the pointer (e.g., 8 bytes on 64-bit systems), not the total array size. - Input Limitations: Using
scanfwith %s stops reading upon encountering whitespace (spaces, tabs, newlines).
4. Handling Input Buffer Issues
When mixing scanf with character input, previous newline characters may remain in the buffer. A robust method to clear the buffer involves consuming characters until a newline is found:
int clear_buffer(void) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
return 0;
}
5. String Manipulation Functions
Note: gets() is deprecated in C11 due to security risks and lacks bounds checking.
strlen: Returns the length of the string excluding the null terminator (\0).strcpyandstrcat:- The destination argument must point to a writable memory block (cannot be a string literal).
strcatappends the source string to the destination buffer.strcpyoverwrites the destination entirely. If the destination buffer is smaller than the source, it results in undefined behavior/buffer overflow. Even if the destination is large enough, any existing content beyond the length of the new string is effectively overwrittan or ignored.
strcmp: Compares two strings lexicographically based on ASCII values. Returns 0 if equal, a positive integer if the first differs by a greater value, and a negative integer otherwise.
6. Pointers and Dynamic Memory
- Pointer Size: In a 64-bit enviroment, pointers are typically 8 bytes. In a 32-bit environment, they are 4 bytes.
- Dynamic Allocation (
malloc): Requires including<stdlib.h>. Memory must be manually freed usingfree().
Corrected Implementation Examples:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
// Example 1: Character Buffer
char *dynamic_str = malloc(10 * sizeof(char));
if (dynamic_str != NULL) {
strcpy(dynamic_str, "Hello");
printf("String: %s\n", dynamic_str);
free(dynamic_str);
}
// Example 2: Integer Array
int count = 3;
int *numbers = malloc(count * sizeof(int));
if (numbers != NULL) {
numbers[0] = 10;
numbers[1] = 20;
printf("First Element: %d\n", numbers[0]);
free(numbers);
}
return 0;
}
- Void Pointers: Cannot be dereferenced or incremented directly. They often require casting before use.
- Null Termination: When allocating space for strings, always account for one extra byte for the
\0terminator.
7. Structure Memory Alignment
The total size of a struct is not simply the sum of its members' sizes. The compiler applies padding to align data to memory boundaries for optimization. The structure size is usual a multiple of the largest member's alignment requirement.
8. Fundamental Data Structures Logic
- Stack: Typically implemented with an index starting at 0. If the maximum capacity is
max_size, the stack is full when the top index reachesmax_size - 1. - Queue:
- Circular Queue: Both front and rear indices initialize to 0. The
frontpoints to the actual start element, whilerearpoints to the position immediately following the last element. - Linked List Queue: Head and tail pointers initialize to
NULL. The head points to the first node, and the tail points to the last node.
- Circular Queue: Both front and rear indices initialize to 0. The