C programming, while simple and structured, provides powerful tools and
concepts for advanced-level programming. These advanced topics enhance your
understanding of the language and prepare you for complex programming
challenges.
1. Bitwise Operations
Bitwise operators manipulate individual bits of data, allowing low-level data
processing and optimization.
Bitwise AND (&)
Bitwise OR (|)
Bitwise XOR (^)
Bitwise NOT (~)
Left Shift (<<)
Right Shift (>>)
Example: Bit Manipulation
#include <stdio.h>
int main() {
unsigned int a = 5; // Binary: 0101
unsigned int b = 3; // Binary: 0011
printf("a & b = %u\n", a & b); // 0001
printf("a | b = %u\n", a | b); // 0111
printf("a ^ b = %u\n", a ^ b); // 0110
printf("~a = %u\n", ~a); // Complement of 0101
return 0;
}
, 2. Dynamic Memory Allocation
Dynamic memory allocation allows programs to request memory at runtime using
functions from <stdlib.h>.
malloc(): Allocates memory but does not initialize it.
calloc(): Allocates and initializes memory.
realloc(): Resizes previously allocated memory.
free(): Frees allocated memory.
Example: Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*)malloc(5 * sizeof(int)); // Allocate memory for 5 integers
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
free(arr); // Free the allocated memory
return 0;
}