C programming interview questions along with their answers:
- What is the difference between
malloc()
andcalloc()
functions in C?- Answer:
malloc()
: Themalloc()
function is used to dynamically allocate memory in C. It takes a single argument representing the number of bytes to allocate and returns a pointer to the allocated memory block. The memory allocated bymalloc()
is uninitialized, meaning it contains garbage values.calloc()
: Thecalloc()
function is also used for dynamic memory allocation in C. It takes two arguments representing the number of elements and the size of each element, and it initializes the allocated memory block to zero. Unlikemalloc()
, which requires manual initialization,calloc()
automatically initializes the memory to zero, making it useful for initializing arrays and data structures.
- Answer:
- Explain the difference between
printf()
andsprintf()
functions in C.- Answer:
printf()
: Theprintf()
function in C is used to print formatted output to the standard output stream (usually the console). It takes a format string as its first argument, followed by additional arguments corresponding to the format specifiers in the format string. The formatted output is displayed on the console.sprintf()
: Thesprintf()
function is similar toprintf()
but instead of printing the formatted output to the console, it stores the output as a string in a character array (string). It takes a character array (string) as its first argument, followed by a format string and additional arguments. The formatted output is stored in the specified character array.
- Answer:
- What is a pointer in C? Explain with an example.
- Answer:
- A pointer in C is a variable that stores the memory address of another variable. Pointers are used to manipulate memory and facilitate dynamic memory allocation, passing addresses as function arguments, and accessing data structures like arrays and linked lists.
- Example:
c
int main() {
int num = 10;
int *ptr; // Pointer declaration
ptr = # // Pointer initialization with address of 'num'
printf("Value of num: %d\n", num); // Output: Value of num: 10
printf("Address of num: %p\n", &num); // Output: Address of num: <address>
printf("Value of num using pointer: %d\n", *ptr); // Output: Value of num using pointer: 10
printf("Address stored in pointer: %p\n", ptr); // Output: Address stored in pointer: <address>
return 0;
}
- Answer:
- What is the difference between
++i
andi++
in C?- Answer:
++i
(pre-increment): The++i
operator increments the value of the variablei
by 1 before its current value is used in the expression. It returns the incremented value ofi
.i++
(post-increment): Thei++
operator increments the value of the variablei
by 1 after its current value is used in the expression. It returns the original value ofi
.- Example:
c
int i = 5;
int a = ++i; // a = 6, i = 6 (incremented before assigning to 'a')
int b = i++; // b = 6, i = 7 (incremented after assigning to 'b')
- Answer:
- Explain the difference between
strcmp()
andstrncmp()
functions in C.- Answer:
strcmp()
: Thestrcmp()
function is used to compare two strings in C. It takes two null-terminated strings as arguments and returns an integer value indicating their lexicographical relationship. It returns 0 if the strings are equal, a negative value if the first string is lexicographically less than the second string, and a positive value if the first string is lexicographically greater than the second string.strncmp()
: Thestrncmp()
function is similar tostrcmp()
but allows specifying the maximum number of characters to compare. It takes three arguments: two strings to compare and the maximum number of characters to compare. It returns the same integer values asstrcmp()
, but the comparison is limited to the specified number of characters.- Example:
c
int main() {
char str1[] = "hello";
char str2[] = "world";
int result1 = strcmp(str1, str2); // Result: -1 (str1 < str2)
int result2 = strncmp(str1, str2, 3); // Result: 0 (first 3 characters are equal)
printf("Result of strcmp(): %d\n", result1);
printf("Result of strncmp(): %d\n", result2);
return 0;
}
- Answer: