Top 20 Basic C Programming VIVA  Questions and Answers

Top 20 Basic C Programming VIVA  Questions and Answers


🌐 Dive into the Heart of C Programming: Uncover the Top 20 Basic VIVA Questions and Expert Answers! 💡

Prepare to showcase your C programming prowess with our curated list of the Top 20 Basic C Programming VIVA Questions and Answers. Whether you're a student gearing up for exams or a professional looking to refresh your knowledge, this comprehensive guide is tailored to enhance your understanding and boost your confidence during VIVA sessions.


Questions

  1. What is the purpose of the register keyword in C?

    • The register keyword suggests to the compiler that a variable is likely to be heavily used and should be stored in a CPU register if possible. However, modern compilers often optimize this automatically, making the use of register less impactful.

  2. Explain the concept of typecasting in C.

    • Typecasting involves converting a variable from one data type to another. For example, (float) 5 performs typecasting to a float.

  3. What is the role of the const keyword in a function parameter list?

    • In a function parameter list, the const keyword is used to indicate that the parameter is treated as a constant and cannot be modified within the function.

  4. Discuss the purpose of the #ifdef and #ifndef directives in C.

    • #ifdef checks if a macro is defined, while #ifndef checks if a macro is not defined. They are commonly used for conditional compilation.

  5. What is the purpose of the enum keyword in C?

    • The enum keyword is used to define an enumeration type, which represents a set of named integer constants.

  6. Explain the difference between local and global static variables.

    • Local static variables are declared inside a function and retain their values between function calls. Global static variables are declared outside any function and are visible only within the file they are declared in.

  7. How do you define a macro in C using the #define directive?

    • A macro is defined using the #define directive, like this: #define MAX_SIZE 100.

  8. What is the purpose of the sizeof operator when used with a data type?

    • The sizeof operator returns the size, in bytes, of a data type. For example, sizeof(int) returns the size of an integer.

  9. How can you check if a number is odd or even in C without using the modulo operator?

    • You can use the bitwise AND operator: if (num & 1) { /* odd */ } else { /* even */ }.

  10. What is a function pointer, and how is it used in C?

    • A function pointer is a variable that stores the address of a function. It can be used to call functions dynamically at runtime.

  11. Explain the purpose of the restrict keyword in C.

    • The restrict keyword is a hint to the compiler that a pointer is the only reference to the data it points to, which can enable optimizations.

  12. How do you dynamically allocate a 2D array in C using pointers?

    • You can use a pointer to a pointer (int**) and dynamic memory allocation functions (malloc, calloc) to create a dynamic 2D array.

  13. Discuss the role of the offsetof macro in C.

    • The offsetof macro is used to find the offset of a member within a structure.

  14. What is a union, and how is it different from a structure?

    • A union is a data structure that allows different data types to be stored in the same memory location. Unlike structures, where each member has its own memory location, union members share the same memory.

  15. Explain the purpose of the volatile keyword in C.

    • The volatile keyword is used to indicate that a variable's value may change at any time outside the program's control, preventing certain compiler optimizations.

  16. What is the purpose of the inline keyword in C?

    • The inline keyword suggests to the compiler that it should attempt to integrate the function's code directly into the calling code rather than performing a traditional function call.

  17. Discuss the use of the __FILE__ and __LINE__ macros in C.

    • __FILE__ is a predefined macro that expands to the current source file name, and __LINE__ expands to the current line number.

  18. Explain the purpose of the EXIT_SUCCESS and EXIT_FAILURE macros.

    • These macros are used as exit codes in C programs. EXIT_SUCCESS typically represents a successful program execution, and EXIT_FAILURE represents a failure.

  19. What is the purpose of the assert macro in C?

    • The assert macro is used for debugging purposes. It tests an expression and, if false, prints a diagnostic message and terminates the program.

  20. Discuss the concept of a pointer to a function in C.

    • A pointer to a function stores the memory address of a function. It allows for dynamic function calls and is commonly used in scenarios like callbacks and function arrays.