C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It's widely used for system programming, embedded systems, and developing operating systems due to its efficiency and control over hardware.
The main features of C language include:
malloc()
and calloc()
.
malloc(size_t size)
allocates a block of memory of a specified size but doesn't initialize it, while calloc(size_t n, size_t size)
allocates memory for an array of n
elements and initializes all bytes to zero.
++i
and i++
?
++i
(pre-increment) increments the value of i
before using it in an expression, while i++
(post-increment) uses the value of i
in the expression and then increments it.
A pointer is a variable that stores the memory address of another variable. Pointers are powerful tools in C for dynamic memory allocation, arrays, and functions.
Dynamic memory allocation allows the program to allocate memory during runtime using functions like malloc()
, calloc()
, realloc()
, and free()
.
A memory leak occurs when dynamically allocated memory is not freed, leading to a waste of memory resources. It can be prevented by ensuring every malloc()
or calloc()
call has a corresponding free()
call.
sizeof
operator.
The sizeof
operator in C returns the size (in bytes) of a data type or object.
This can be done by iteratively changing the next pointers of the nodes or using recursion.
A stack can be implemented using an array where the top of the stack is managed using an index. Operations like push, pop, and peek are performed based on this index.
Function pointers are pointers that point to the address of a function. They are used for callback functions, implementing function tables, and creating dynamic dispatch mechanisms.
Bitwise operators perform operations at the bit level (AND, OR, XOR, NOT, etc.). They are used in tasks that involve bit manipulation, such as setting or clearing flags, encryption, and compressing data.
A structure allocates separate memory for each of its members, whereas a union allocates a single memory location shared by all its members. The size of a union is determined by its largest member.
struct
and typedef struct
in C?
struct
defines a structure type. Using typedef struct
allows you to create an alias for the structure, so you can avoid using the struct
keyword when declaring variables of that type.
A segmentation fault occurs when a program attempts to access memory that it is not allowed to, often due to dereferencing a null or uninitialized pointer.
assert
macro work?
The assert
macro is used for debugging purposes. It checks a condition and if the condition is false, it displays an error message and terminates the program.
The program should calculate the factorial of a given number using either a loop or recursion.
The program should take a string input and reverse it by manually swapping characters.
The program should iterate through the array, keeping track of the maximum value encountered.
The program should check if the given integer reads the same forwards and backwards.
The program should traverse the array, keeping track of the largest and second-largest elements.
Write a program that eliminates duplicates from an unsorted array, ideally without using extra memory for another array.
k
positions.
The function should modify the array in place to shift all elements k
positions to the right.
An anagram is a word formed by rearranging the letters of another word.
The input is a string, and the output should be the string with words in reverse order.
Use Floyd’s Cycle detection algorithm (tortoise and hare) to determine if a cycle exists.
The function should take two pointers to the heads of the lists and return a pointer to the head of the merged list.
Use two pointers, one moving twice as fast as the other. When the fast pointer reaches the end, the slow pointer will be at the middle.
The program should efficiently count the bits set to 1 in the integer's binary form.
Implement this using bitwise XOR.