C Programming Viva Questions
-
What is input and output in C?
Input and output (I/O) in C refers to the process of receiving data from external sources (input) and sending data to external destinations (output). Standard I/O functions like `printf` and `scanf` are commonly used for text-based I/O.
-
Explain the `printf` function in C with an example.
`printf` is used for formatted output to the console. It takes a format string containing placeholders for variables, which are replaced during execution.
#include <stdio.h> int main() { int age = 25; float height = 5.9; // Displaying variables using printf printf("Age: %d, Height: %.2f\\n", age, height); return 0; }
-
Discuss the format specifiers used in `printf`.
Format specifiers in `printf` define the type and format of the variable being displayed. Common specifiers include `%d` for integers, `%f` for floating-point numbers, `%c` for characters, and `%s` for strings.
-
Explain the `scanf` function in C with an example.
`scanf` is used for formatted input from the console. It takes a format string with placeholders corresponding to variables where the input values will be stored.
#include <stdio.h> int main() { int age; float height; // Accepting input using scanf printf("Enter your age: "); scanf("%d", &age); printf("Enter your height: "); scanf("%f", &height); // Displaying the entered values printf("Entered values - Age: %d, Height: %.2f\\n", age, height); return 0; }
-
Discuss the concept of file I/O in C.
File I/O in C involves reading from and writing to files using functions like `fscanf`, `fprintf`, `fgets`, and `fputs`. It provides a way to interact with external files for data storage and retrieval.
-
Explain the concept of standard streams in C.
Standard streams in C are I/O channels that are automatically created when a C program starts. They are:
- `stdin` (Standard Input): Read data from the keyboard.
- `stdout` (Standard Output): Display data on the console.
- `stderr` (Standard Error): Display error messages on the console.
-
Provide an example of redirecting standard streams in C.
Redirecting standard streams allows input to come from a file instead of the keyboard or sends output to a file instead of the console. This can be achieved using shell commands or by changing the file descriptors in the C program.
#include <stdio.h> int main() { // Redirecting stdin to read from a file freopen("input.txt", "r", stdin); // Redirecting stdout to write to a file freopen("output.txt", "w", stdout); // Your program logic here return 0; }
-
Discuss the role of `getchar` and `putchar` in C.
`getchar` and `putchar` are functions for reading and writing single characters to the console. They are commonly used for simple character-based I/O operations.
-
Provide an example of using `getchar` and `putchar` in C.
#include <stdio.h> int main() { char character; // Using getchar to read a character printf("Enter a character: "); character = getchar(); // Using putchar to display the entered character printf("You entered: "); putchar(character); return 0; }
-
Discuss common pitfalls and best practices in I/O operations in C.
Common pitfalls include not handling errors in I/O operations, improper use of format specifiers, and neglecting to close files after use. Best practices involve robust error checking, using appropriate format specifiers, and ensuring proper file closure.
C Programming Viva Questions
-
What is a variable in C, and how is it declared?
A variable in C is a named memory location used to store data. It's declared with a data type and a unique identifier, like
int count;
. -
Explain the concept of data types in C variables.
Data types in C define the type of data a variable can hold. Examples include
int
,float
,char
, specifying the nature of the stored value. -
How does the scope of a variable affect its visibility in C?
The scope of a variable determines where it can be accessed. Local variables are limited to the block they're declared in, while global variables have broader accessibility.
-
Discuss the differences between local and global variables in C.
Local variables are confined to a specific function or block, while global variables can be accessed throughout the entire program.
-
What is the significance of the `const` keyword when declaring variables?
The `const` keyword in C indicates that a variable's value cannot be modified once assigned, providing a form of constant or read-only variable.
-
How do you initialize a variable during its declaration in C?
Initialization is done by assigning a value during declaration, like
int x = 10;
. It associates a value with the variable from the beginning. -
Explain the concept of dynamic memory allocation for variables in C.
Dynamic memory allocation, using functions like
malloc
, allows variables to be assigned memory during runtime, providing flexibility in memory usage. -
Discuss the role of the `sizeof` operator in determining variable size.
sizeof
in C returns the size, in bytes, of a variable or data type, aiding in memory management and alignment considerations. -
How can you swap the values of two variables without using a temporary variable?
Swapping without a temporary variable is possible using arithmetic or bitwise operations, like
a = a + b; b = a - b; a = a - b;
. -
What is the purpose of the `extern` keyword when dealing with variables in C?
extern
declares a variable without defining it, indicating the variable is defined elsewhere, often in another source file or library.