A common interview problem involves counting the number of 1 bits (set bits) in the binary representation of an integer. The key insight lies in understanding how computers store signed integers using two's complement notation. The expression n &= (n - 1) is a powerful optimization that eliminat...
LinkedList Header Definition The underlying data structure is a singly linked list. We define the node structure to hold generic data, which will be specialized for the contact information later. #ifndef LINKED_LIST_H #define LINKED_LIST_H #include <stdlib.h> #include <assert.h> #include...
1D and 2D Integer Array Memory Layout Analysis #include <stdio.h> #define ROWS 2 #define COLS 4 void analyze_single_dim() { int values[COLS] = {1, 9, 8, 4}; int idx; printf("Total size of values: %lu bytes\n", sizeof(values)); for (idx = 0; idx < COLS; idx++) printf("Address...
File Character and Line Coutning #include <stdio.h> #include <ctype.h> #define MAX_SIZE 100 int main() { FILE* file_ptr; file_ptr = fopen("input.txt", "r"); if (!file_ptr) { perror("File opening failed"); return 1; } int line_count = 0; int char_count = 0; c...
Register Storage Class in C The register keyword in C serves as a storage class specifier that suggests the compiler to store a variable in a CPU register for faster access. Register variables are typically used for frequently accessed data to optimize performence. Purpose and Functionality CPU regi...
1. Formatted Output with printf() The printf() function delivers formatted text to the standard output stream. The 'f' in its name signifies "format," meaning you can control how the output appears. Crucially, printf() does not append a newline automatically; the cursor remains at the end...
C Programming Exercises and Solutions 1. Greatest Common Divisor and Least Common Multiple #include #include int main() { int calculateGCD(int first, int second); int calculateLCM(int first, int second); int num1, num2, temp; printf("Enter two numbers:\n"); scanf("%d%d", &num1, &num2); if (num1 < nu...
Binary Operators Binary operators require two operands for evaluation. Multiplication (*) #include <stdio.h> int main(void) { int val = 5; printf("%d\n", val * val); return 0; } Division (/) Integer division truncates fractional parts. #include <stdio.h> int main(void) { float...
Arithmetic Operators C language provides several arithmetic operators for performing mathematical operations on variables: + - * / % All operators except % can work with both integers and floating-point numbers. For the / operator, if both operands are integers, integer division is performed. If at...
Palindrome Validation with Bidirectional Traversal Determining whether a sequence reads identically from both directions can be achieved without auxiliary buffers by employing converging indices: #include <stdio.h> #include <stdbool.h> #include <string.h> #define BUFFER_SIZE 100 bo...