C Programming Viva Questions With Answers C Programming Viva Questions with Answers A Comprehensive Guide This guide provides a comprehensive overview of frequently asked C programming viva questions categorized for clarity and enhanced understanding It includes answers stepby step explanations best practices and common pitfalls to help you ace your viva examination Well cover fundamental concepts memory management pointers data structures and more C Programming Viva Questions Answers Interview Questions C Language Data Structures Pointers Memory Management Programming Fundamentals Best Practices Common Pitfalls I Fundamental Concepts 1 What is C Programming C is a structured generalpurpose programming language known for its efficiency and portability Its widely used in system programming embedded systems and game development Its a procedural language meaning it emphasizes procedures or functions 2 Explain the difference between int float and char data types int Represents integer values whole numbers like 10 0 100 float Represents singleprecision floatingpoint numbers numbers with decimal points like 314 25 char Represents single characters like A b 3 What are keywords in C Give examples Keywords are reserved words with predefined meanings in C They cannot be used as variable names or identifiers Examples include int float char if else while for return 4 What is the purpose of the main function The main function is the entry point of any C program Execution begins from the main function 5 Explain the difference between and 2 Assignment operator assigns a value to a variable eg x 5 Equality operator compares two values for equality eg if x 5 Inequality operator checks if two values are not equal eg if x 5 II Control Structures and Loops 1 Explain different types of loops in C C provides three main loop types for loop Used for iterating a specific number of times c for int i 0 i 18 printfEligible to voten else printfNot eligible to voten 3 What is a switch statement A switch statement provides a more efficient way to handle multiple conditional branches based on the value of an integer expression c int day 3 switch day case 1 printfMondayn break case 2 printfTuesdayn break case 3 printfWednesdayn break default printfOther dayn III Pointers and Memory Management 1 What is a pointer A pointer is a variable that stores the memory address of another variable Its declared using the symbol c int x 10 int ptr x ptr stores the address of x 2 Explain the difference between and operators Addressof operator returns the memory address of a variable Dereference operator accesses the value stored at the memory address pointed to by a pointer 4 3 What is dynamic memory allocation Dynamic memory allocation allows allocating memory during runtime using functions like malloc calloc and realloc This is crucial when the amount of memory needed is not known beforehand 4 What are malloc calloc and free malloc Allocates a block of memory of specified size calloc Allocates a block of memory for a specified number of elements each of specified size and initializes them to zero free Releases dynamically allocated memory back to the system Failure to free allocated memory leads to memory leaks IV Arrays and Strings 1 What is an array An array is a contiguous block of memory that stores elements of the same data type 2 What is a string in C A string in C is a nullterminated sequence of characters The null character 0 marks the end of the string 3 How to declare and initialize an array c int numbers5 1 2 3 4 5 Declaration and initialization char name10 John String initialization 4 How to access array elements Array elements are accessed using their index starting from 0 For example numbers0 accesses the first element 1 V Functions and Structures 1 What is a function A function is a block of code that performs a specific task It improves code organization and reusability 2 Explain function prototypes 5 Function prototypes declare the functions return type name and parameters before its definition This allows the compiler to check for type errors 3 What is a structure A structure groups variables of different data types under a single name 4 How to declare and access members of a structure c struct Student char name50 int rollNo float marks struct Student s1 strcpys1name Alice s1rollNo 101 s1marks 855 VI Common Pitfalls to Avoid Uninitialized variables Always initialize variables before use Offbyone errors Carefully check loop boundaries and array indices Memory leaks Always free dynamically allocated memory Dangling pointers Avoid using pointers to memory that has been freed Buffer overflows Ensure sufficient buffer size to avoid writing beyond allocated memory VII Summary This guide covered fundamental C programming concepts crucial for acing your viva examination Understanding pointers memory management control structures and data structures is key Remember to practice coding and focus on avoiding common pitfalls VIII FAQs 1 What are the advantages of using pointers in C Pointers offer several advantages dynamic memory allocation efficient data manipulation especially with arrays and structures passing large data structures to functions efficiently and implementing data structures like linked lists and trees 6 2 How can I handle errors during dynamic memory allocation Always check the return value of malloc and calloc If they return NULL it indicates memory allocation failure Handle this appropriately perhaps by printing an error message and exiting the program or attempting alternative strategies 3 What is the difference between a local and global variable A local variable is declared inside a function and is only accessible within that function A global variable is declared outside any function and is accessible from any function in the program Overuse of global variables can reduce code clarity and maintainability 4 Explain the concept of recursion in C Recursion is a programming technique where a function calls itself directly or indirectly Its useful for solving problems that can be broken down into smaller selfsimilar subproblems like traversing tree structures or calculating factorials However improper use can lead to stack overflow errors 5 What are header files and why are they important Header files h contain function prototypes macro definitions and data structure declarations They are included in source code files using include directives They improve code organization reusability and allow modular programming by separating interface header from implementation source code Standard header files like stdioh provide access to standard inputoutput functions