C programming Viva Questions with Answers

C programming Viva Questions With Answers

Basics of C Programming

Q: What is C programming?

A: C programming is a high-level programming language used for developing software applications.

Q: Who developed the C programming language?

A: C was developed by Dennis Ritchie at Bell Labs in the early 1970s.

Q: What is the difference between a compiler and an interpreter?

A: A compiler translates the entire program into machine code before execution, while an interpreter translates and executes code line by line.

Q: What is a header file in C?

A: A header file is a file containing declarations for functions and variables that can be used in other C files.

Q: Explain the structure of a C program.

A: A C program generally consists of a preprocessor directive, main function, and user-defined functions.

Q: What is the significance of the “return 0;” statement in the main function?

A: The “return 0;” statement in the main function indicates a successful program execution and is often used to signal that the program was completed without errors.

Q: Explain the concept of comments in C.

A: Comments in C are used to add explanatory notes to the code for documentation purposes. They are ignored by the compiler and start with “//” for single-line comments or are enclosed between “/” and “/” for multi-line comments.

Q: What is the purpose of the sizeof operator in C?

A: The sizeof operator is used to determine the size, in bytes, of a data type or a variable, which can be helpful for memory allocation and buffer management.

Q: What are escape sequences in C?

A: Escape sequences are special character combinations that begin with a backslash “” and are used to represent characters that are difficult to include directly in a string, such as newline (“\n”) or tab (“\t”).

Data Types and Variables

Q: What is a variable?

A: A variable is a named storage location in a program that can hold different values during its execution. It is declared with a name, has a data type, and can change its value. In Simple words, a variable is a container for storing data in a program.

Q: What is a constant?

A constant is a value that does not change during program execution. It can be either named (with a symbolic name) or literal (a specific value used directly in code).

Q: Name the basic data types in C.

A: int, float, char, double.

Q: What is the size of the int data type in C?

A: It depends on the platform, but it’s usually 4 bytes.

Q: What is the size of the char data type in C?

A: Typically, 1 byte.

Q: How do you declare a constant in C?

A: Using the ‘const‘ keyword.

Operators and Expressions

Q: What is an operator in C?

A: Operators are symbols used to perform operations on variables and values.

Q: Differentiate between unary, binary, and ternary operators.

A: Unary operators work on a single operand, binary on two, and ternary on three.

Q: Explain the difference between “++i” and “i++” in C.

A: “++i” is a pre-increment operator, while “i++” is a post-increment operator.

Q: What is the ternary conditional operator in C?

A: It’s a shorthand way of writing an if-else statement in a single line.

Q: Explain the purpose of the modulus (%) operator.

A: It gives the remainder when one number is divided by another.

Control Flow Statements

Q: What is an if statement in C?

A: It is used for conditional branching in a program.

Q: What is the purpose of the “switch” statement?

A: It is used for multi-way branching based on the value of an expression.

Q: What is a loop in C?

A: A loop is a control structure that allows you to execute a block of code repeatedly.

Q: Differentiate between “while,” “do-while,” and “for” loops.

A: “while” tests the condition before execution, “do-while” tests it after, and “for” has initialization, condition, and update expressions.

Q: What is a nested loop in C?

A: A nested loop is a loop that is contained inside another loop. It is commonly used for iterating over multi-dimensional arrays or for implementing complex patterns.

Q: How do you exit a program prematurely in C?

A: You can use the “exit” function from the <stdlib.h> library to terminate the program at any point, providing an exit status code.

Q: What is the difference between “break” and “continue” statements in C?

A: The “break” statement is used to exit a loop or switch statement prematurely, while the “continue” statement is used to skip the current iteration of a loop and proceed to the next iteration.

Q: Explain the purpose of the “goto” statement in C.

A: The “goto” statement allows you to transfer control to a labeled statement within the same function. However, it is generally discouraged because it can lead to unreadable and unmaintainable code.

Functions

Q: What is a function in C?

A: A function is a block of code that performs a specific task.

Q: What is the difference between a function declaration and a function definition?

A: A declaration tells the compiler about the function’s name and return type, while a definition provides the actual function implementation.

Q: How do you pass parameters to a function in C?

A: By defining function parameters in its parentheses.

Q: What is recursion in C?

A: Recursion is a technique where a function calls itself to solve a problem.

Q: Explain the difference between “call by value” and “call by reference.”

A: In call by value, a copy of the argument is passed to the function, while in call by reference, the actual argument’s memory location is passed.

Arrays and Pointers

Q: What is an array in C?

A: An array is a collection of elements of the same data type stored in contiguous memory locations.

Q: How do you access elements of an array?

A: By using the array name followed by the index in square brackets.

Q: What is a pointer in C?

A: A pointer is a variable that stores the memory address of another variable.

Q: What is the difference between “int* ptr” and “int *ptr”?

A: There’s no difference; both declare a pointer to an integer.

Q: What is a null pointer in C?

A: A pointer that doesn’t point to any memory location is called a null pointer.

Structures and Unions

Q: What is a structure in C?

A: A structure is a user-defined data type that groups variables of different data types.

Q: How do you access members of a structure?

A: By using the dot operator (.)

Q: What is a union in C?

A: A union is similar to a structure but shares memory among its members.

Q: What is the difference between a structure and a union?

A: In a structure, each member has its memory space, while in a union, all members share the same memory space.

Q: When is a structure used, and when is a union used?

A: Structures are used when you need to store multiple pieces of data, and unions are used when you want to save memory by sharing storage among different types.

File Handling

Q: What is file handling in C?

A: File handling is the process of reading from and writing to files using C.

Q: How do you open a file in C?

A: By using the fopen() function.

Q: How do you close a file in C?

A: By using the fclose() function.

Q: Explain the difference between text files and binary files.

A: Text files contain human-readable text, while binary files store data in a format that is not human-readable.

Q: How do you read from a text file in C?

A: Using functions like ‘fscanf()‘ or ‘fgets()‘.

Dynamic Memory Allocation

Q: What is dynamic memory allocation in C?

A: Dynamic memory allocation allows you to allocate memory during program execution.

Q: How do you allocate memory dynamically in C?

A: By using functions like malloc(), calloc(), or realloc().

Q: How do you deallocate memory in C?

A: By using the free() function.

Q: Explain memory leak in C.

A: Memory leak occurs when allocated memory is not deallocated, causing the program to consume more memory over time.

Q: What is the difference between malloc() and calloc()?

A: malloc() allocates uninitialized memory, while calloc() allocates zero-initialized memory.

Preprocessor Directives

Q: What is a preprocessor directive in C?

A: Preprocessor directives are commands that are processed by the preprocessor before actual compilation.

Q: What is the purpose of the #include directive?

A: It is used to include header files in the source code.

Q: What is the purpose of the #define directive?

A: It is used for creating macros and constants.

Q: Explain conditional compilation in C.

A: Conditional compilation allows you to include or exclude code based on preprocessor conditions.

Q: What is the significance of the #ifdef and #ifndef directives?

A: #ifdef checks if a macro is defined, and #ifndef checks if it’s not defined.

Leave a Reply

Scroll to Top