Top questions with answers asked in MNC on C Programming

C programming interview questions along with their answers:

  1. What is the difference between malloc() and calloc() functions in C?
    • Answer:
      • malloc(): The malloc() 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 by malloc() is uninitialized, meaning it contains garbage values.
      • calloc(): The calloc() 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. Unlike malloc(), which requires manual initialization, calloc() automatically initializes the memory to zero, making it useful for initializing arrays and data structures.
  2. Explain the difference between printf() and sprintf() functions in C.
    • Answer:
      • printf(): The printf() 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(): The sprintf() function is similar to printf() 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.
  3. 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

        #include <stdio.h>
        int main() {
        int num = 10;
        int *ptr; // Pointer declaration
        ptr = &num; // 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;
        }
  4. What is the difference between ++i and i++ in C?
    • Answer:
      • ++i (pre-increment): The ++i operator increments the value of the variable i by 1 before its current value is used in the expression. It returns the incremented value of i.
      • i++ (post-increment): The i++ operator increments the value of the variable i by 1 after its current value is used in the expression. It returns the original value of i.
      • 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')
  5. Explain the difference between strcmp() and strncmp() functions in C.
    • Answer:
      • strcmp(): The strcmp() 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(): The strncmp() function is similar to strcmp() 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 as strcmp(), but the comparison is limited to the specified number of characters.
      • Example:

        c

        #include <stdio.h>
        #include <string.h>
        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;
        }