Post your need

C Programming Interview Questions

  • What are the advantages and disadvantages of C language?

    Advantages:

    C is a simple and easy to learn language that has a small and consistent set of keywords and syntax.

    C is a portable language that can run on different platforms and architectures with minimal changes.

    C is a fast and efficient language that can produce optimized and compact code that can execute close to the hardware level.

    C is a flexible and powerful language that can manipulate the memory and hardware directly using pointers and bitwise operators.

    C is a versatile and expressive language that can support multiple programming paradigms, such as procedural, functional, and object-oriented.

    Disadvantages:

    C is a low-level language that does not provide many built-in features and libraries that are available in modern languages, such as exception handling, garbage collection, generics, etc.

    C is an unsafe language that does not perform many checks and validations on the data and memory, which can lead to errors, bugs, and security issues, such as buffer overflow, memory leak, segmentation fault, etc.

    C is a complex and difficult language that requires a lot of attention and discipline from the programmer, especially when dealing with pointers, memory management, and dynamic allocation.

    C is a verbose and tedious language that requires a lot of code and repetition to achieve simple tasks, such as string manipulation, file handling, data structures, etc.

    What are the differences between static and dynamic memory allocation in C?

    Static memory allocation is the process of allocating memory for variables at compile time, before the execution of the program. The memory is allocated on the stack segment and the size and location of the memory are fixed and cannot be changed during the program execution. Static memory allocation is done using the keywords auto, static, extern, and register.

    Dynamic memory allocation is the process of allocating memory for variables at run time, during the execution of the program. The memory is allocated on the heap segment and the size and location of the memory can be changed during the program execution. Dynamic memory allocation is done using the library functions malloc(), calloc(), realloc(), and free().

    What are the differences between arrays and pointers in C?

    Arrays are a collection of homogeneous elements that are stored in contiguous memory locations and accessed using an index. Arrays are a derived data type that are derived from the primitive data types, such as int, char, float, etc. Arrays are declared using the syntax data_type array_name[size], where data_type is the type of the elements, array_name is the name of the array, and size is the number of elements in the array.

    Pointers are variables that store the address of another variable or memory location. Pointers are a derived data type that are derived from the address operator &. Pointers are declared using the syntax data_type *pointer_name, where data_type is the type of the variable that the pointer points to, and pointer_name is the name of the pointer. Pointers can be used to access and manipulate the data stored in the memory location that they point to.

    What are the differences between call by value and call by reference in C?

    Call by value is the method of passing arguments to a function by copying the value of the actual parameters to the formal parameters. In this method, any changes made to the formal parameters inside the function do not affect the actual parameters outside the function. Call by value is the default method of passing arguments in C and it is done using the syntax function_name(value), where value is the value of the actual parameter.

    Call by reference is the method of passing arguments to a function by passing the address of the actual parameters to the formal parameters. In this method, any changes made to the formal parameters inside the function also affect the actual parameters outside the function. Call by reference is done using the syntax function_name(&variable), where &variable is the address of the actual parameter.

    What are the differences between structure and union in C?

    Structure is auser-defined data type that can store a group of heterogeneous elements under asingle name. Each element in a structure is called a member and has its owndata type and memory location. The size of a structure is the sum of the sizesof its members. Structure is declared using the keyword struct and the syntaxstruct structure_name {member1; member2; ...};, where structure_name is thename of the structure, and member1, member2, etc. are the members of thestructure.

    Union is auser-defined data type that can store a group of heterogeneous elements under asingle name. Each element in a union is called a member and has its own datatype, but shares the same memory location. The size of a union is the maximumof the sizes of its members. Union is declared using the keyword union and thesyntax union union_name {member1; member2; ...};, where union_name is the nameof the union, and member1, member2, etc. are the members of the union.

    What are the differences between break and continue statements in C?

    Break statement is used to terminate the execution of the current loop or switch statement and transfer the control to the next statement outside the loop or switch. Break statement is used to exit from the loop or switch when a certain condition is met or when the loop or switch is no longer required. Break statement is written as break; and it can be used with any loop or switch statement.

    Continue statement is used to skip the execution of the current iteration of the loop and transfer the control to the next iteration of the loop. Continue statement is used to skip some iterations of the loop when a certain condition is met or when the iteration is not required. Continue statement is written as continue; and it can be used with any loop statement.

    What are the differences between local and global variables in C?

    Local variables are variables that are declared and defined inside a function or a block and are accessible only within that function or block. Local variables are created when the function or block is entered and destroyed when the function or block is exited. Local variables are stored on the stack segment and have automatic storage class by default. Local variables are declared using the syntax data_type variable_name, where data_type is the type of the variable and variable_name is the name of the variable.

    Global variables are variables that are declared and defined outside any function or block and are accessible throughout the program. Global variables are created when the program starts and destroyed when the program ends. Global variables are stored on the data segment and have static storage class by default. Global variables are declared using the syntax data_type variable_name, where data_type is the type of the variable and variable_name is the name of the variable.

    What are the differences between recursion and iteration in C?

    Recursion is the process of calling a function by itself until a base condition is met. Recursion is used to solve problems that have a recursive nature, such as factorial, Fibonacci, tree traversal, etc. Recursion is implemented using a recursive function, which is a function that calls itself within its body. Recursion is written as function name (arguments), where function name is the name of the recursive function and arguments are the parameters passed to the function.

    Iteration is the process of repeating a set of statements until a condition is met. Iteration is used to solve problems that have a repetitive nature, such as counting, sorting, searching, etc. Iteration is implemented using a loop statement, such as for, while, or do-while. Iteration is written as loop statement (condition) {statements;}, where loop statement is the name of the loop statement, condition is the expression that controls the loop, and statements are the statements that are executed inside the loop.

    What are the differences between malloc and calloc functions in C?

    Malloc and calloc are library functions that are used to allocate dynamic memory on the heap segment. Both functions return a pointer to the allocated memory or NULL if the allocation fails. Both functions require the header file stdlib.h to be included in the program.

    Malloc function is used to allocate a single block of memory of a given size in bytes. Malloc function does not initialize the allocated memory and may contain garbage values. Malloc function is written as void *malloc(size_t size), where size is the number of bytes to be allocated.

    Calloc function is used to allocate multiple blocks of memory of a given size in bytes. Calloc function initializes the allocated memory to zero. Calloc function is written as void *calloc(size_t num, size_t size), where num is the number of blocks to be allocated and size is the size of each block in bytes.

    What are the differences between #define and const in C?
    #define and const are both used to declare constants in C, which are values that cannot be changed  
Interested about C Programming?
Get in touch with training experts Get Free Quotes
Leave a comment
Get some additional training from
our expert trainer to learn
Get a job nearby! Upload Resume
  • doc, docx, pdf are allowed
  • US (+1)
Upload your resume