0% found this document useful (0 votes)
38 views9 pages

C

C interview / viva

Uploaded by

abhishes39561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views9 pages

C

C interview / viva

Uploaded by

abhishes39561
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C is a mid-level structured-oriented programming and general-purpose programming.

It is one of the oldest and most popular


programming languages. There are many applications in which C programming language is used, including language-compilers,
operating systems, assemblers and network drivers.

1. Why is C called a mid-level programming language?

C is called a mid-level programming language because it combines the features of both high-level and low-level
languages, allowing for system-level programming as well as application development.

2. What are the features of the C programming language?

Simple and efficient – Easy to learn and produces efficient programs.


Machine Independent – Code can run on different machines with minimal modification.
Structured Language – Supports structured programming with functions and control structures.
Rich Library – Provides a wide range of built-in functions.
Memory Management – Allows dynamic memory allocation and pointer manipulation.
Low-level Access – Can directly access hardware using pointers.
Fast Execution – Offers high performance due to its close-to-hardware nature.
Modular Programming – Programs can be divided into functions for easier management.
Portability – Code written in C can be transferred from one platform to another.
Recursion Support – Allows functions to call themselves for repetitive tasks.

3. What are basic data types supported in the C Programming Language?

4. What are tokens in C?

Tokens in C are the smallest individual units in a C program that the compiler recognizes. They include:
• Keywords – Reserved words (e.g., int, return, if)
• Identifiers – Names of variables, functions, arrays, etc.
• Constants – Fixed values like numbers (10, 3.14) or characters ('A')

5. What do you mean by the scope of the variable?

The scope of a variable in C refers to the region or part of the program where the variable is declared and accessible.
It determines where the variable can be used or modified.
6. What is the use of static variables in C?

In C, static variables are used to retain their value between function calls and to restrict the scope of a variable to the
file or function where it is declared.
• Inside a function: A static variable keeps its value between multiple calls to that function.
• Outside a function: It limits the variable's scope to the current file, preventing access from other files.

7. What are preprocessor directives in C?

Preprocessor directives in C are instructions that are processed by the preprocessor before the actual compilation of
the code begins. They begin with the symbol #.
Common preprocessor directives include:
• #include – Includes header files (e.g., #include <stdio.h>)
• #define – Defines macros or constants (e.g., #define PI 3.14)

8. What is the difference between malloc() and calloc() in the C programming language?

calloc() and malloc() library functions are used to allocate dynamic memory. Dynamic memory is the memory that is
allocated during the runtime of the program from the heap segment. “stdlib.h” is the header file.

9. What do you mean by dangling pointers and how are dangling pointers different from memory leaks in C
programming?

A dangling pointer is a pointer that points to a memory location that has been freed or deallocated. Accessing or
modifying memory through a dangling pointer leads to undefined behavior.

A memory leak occurs when a program fails to release memory that is no longer needed, causing a loss of available
memory over time.

10. What is recursion in C?

Recursion in C is a programming technique where a function calls itself directly or indirectly to solve a problem.

It is commonly used to solve problems that can be broken down into smaller, similar sub-problems
11. What are pointers?

Pointers are variables that store the memory address of another variable. Instead of holding data directly, a pointer
holds the location where the data is stored.

Uses of pointers in C:

• Passing arguments by reference: Allows functions to modify the actual variables by passing their addresses.
• Accessing array elements: Pointers can efficiently traverse arrays by pointing to elements directly.
• Returning multiple values from a function: By passing pointers to a function, multiple results can be updated
and returned.
• Dynamic memory allocation: Pointers are essential to allocate, access, and free memory dynamically during
program execution.

12. What is typedef in C?

typedef in C is a keyword used to create new names (aliases) for existing data types, making code easier to read and
manage.

13. What are loops in C?

Loops are control structures that allow you to execute a block of code

repeatedly based on a condition. They help automate repetitive tasks.

Common types of loops in C:

• for loop
• while loop
• do-while loop
14. What are header files and their uses?

Header files in C are files with .h extension that contain function declarations, macro definitions, constants, and type
definitions. They allow you to share common code (like library functions) across multiple source files.

Uses:

• Provide declarations to be shared across files

• Enable code modularity and reusability

• Include standard libraries like stdio.h, stdlib.h

15. What are functions and their types?

Functions are blocks of code designed to perform a specific task, which can be called multiple times throughout a
program.

Types of functions:

• Library Functions: Predefined functions in C standard libraries (e.g., printf(), scanf())

• User-defined Functions: Functions created by the programmer to perform custom tasks

16. What is the difference between macro and functions?

17. How to convert a string to numbers in C?

Use standard library functions like:

• atoi() — converts string to int

• atof() — converts string to float

• strtol() — converts string to long with error checking

Example:
18. What are reserved keywords?

Reserved keywords are words that have special meaning in C and cannot be used as identifiers (variable/function
names). Examples: int, return, if, while.

19. What is a structure?

A structure (struct) is a user-defined data type that groups variables of different types under a single name.

Example:

20. What is union?

A union is similar to a structure but stores all members at the same memory location, meaning only one member can
hold a value at a time.

21. What is an r-value and l-value?

• l-value: Refers to an object that has a memory address (can appear on the left of assignment)

• r-value: A value or expression that does not have a persistent memory address (usually appears on the right
side)

22. Difference between call by value and call by reference

• Call by value: Passes a copy of the variable; changes inside function don’t affect original

• Call by reference: Passes the address; changes inside function affect original variable

23. What is the sleep() function?

sleep(seconds) pauses program execution for a specified number of seconds (available in <unistd.h> on Unix/Linux).

24. What are enumerations?

enum defines a user-defined type consisting of named integer constants.

Example:
25. What is the volatile keyword?

volatile tells the compiler that a variable’s value may change unexpectedly, preventing optimization on that variable.

26. How is source code different from object code?

• Source code: Human-readable code written by programmer (.c files)

• Object code: Machine code generated by compiler, not human-readable (.obj or .o files)

27. What is static vs dynamic memory allocation?

• Static allocation: Memory allocated at compile time (e.g., global/static variables)

• Dynamic allocation: Memory allocated at runtime using malloc(), calloc(), etc.

28. What is pass-by-reference in functions?

Passing the address of variables to a function so it can modify the original variables directly.

29. What is a memory leak and how to avoid it?

Memory leak occurs when dynamically allocated memory is not freed, causing wasted memory.
Avoid it by: Always using free() for allocated memory once done.

30. What are command line arguments?

Parameters passed to the program at startup via main(int argc, char *argv[]).

31. What is auto keyword?

auto declares automatic local variables (default storage class inside functions); usually omitted as it’s default.

32. Use of extern storage specifier

extern declares a variable defined in another file, allowing access across multiple files.

33. Use of printf() and scanf() and format specifiers

• printf() prints formatted output to the screen.

• scanf() reads formatted input from the user.

• Format specifiers indicate data type, e.g., %d (int), %f (float), %c (char), %s (string).

34. What are near, far, and huge pointers in C?

(Used in older 16-bit systems)

• Near pointer: 16-bit pointer limited to current segment

• Far pointer: 32-bit pointer with segment:offset, can access beyond current segment

• Huge pointer: Like far, but normalized for pointer arithmetic


35. Mention file operations in C

• fopen() — open a file

• fclose() — close a file

• fread() / fwrite() — read/write binary data

• fprintf() / fscanf() — formatted output/input

• fgetc() / fputc() — read/write a character

36. Difference between getc(), getchar(), getch(), and getche()

Function Description Source

getc() Reads a character from a file/stream stdio.h

getchar() Reads a character from standard input (stdin) stdio.h

getch() Reads a character without echoing to screen conio.h (Windows)

getche() Reads a character with echoing to screen conio.h (Windows)


EXTRA

1. Difference between struct and union: A structure allocates separate memory for each member, whereas a union
allocates shared memory for all members.

2. Pointer arithmetic: Pointer arithmetic involves manipulating pointer values to navigate through memory addresses
corresponding to array elements.

3. Segmentation fault: A segmentation fault occurs when a program attempts to access memory that it is not authorized
to access.

4. const keyword: The const qualifier indicates that the value of a variable cannot be modified after initialization.

5. Difference between malloc() and realloc(): malloc() allocates new memory of a specified size, whereas realloc() adjusts
the size of previously allocated memory.

6. Dangling pointer: A dangling pointer refers to a pointer that points to memory that has already been deallocated.

7. Difference between break and continue: The break statement terminates the nearest enclosing loop, while continue
skips the remaining statements in the current iteration and proceeds to the next iteration.

8. sizeof operator: The sizeof operator yields the size in bytes of a data type or variable.

9. Storage classes: Storage classes define the scope, lifetime, and linkage of variables within a program.

10. Difference between volatile and const: The volatile qualifier informs the compiler that a variable’s value may change
unexpectedly, while const prevents modification of a variable’s value.

11. Function pointers: Function pointers store the address of a function and enable dynamic function invocation.

12. Stack vs heap: The stack is used for static memory allocation and function call management, while the heap is used for
dynamic memory allocation.

13. Error handling in C: Error handling in C is commonly performed using return codes, global error indicators such as errno,
or signal handling mechanisms.

14. Macros: Macros are preprocessor directives that perform textual substitution before compilation.

15. Recursion use case caution: Recursion should be used cautiously as it may lead to inefficient memory use and stack
overflow if not properly controlled.

16. Bitwise operators: Bitwise operators perform operations on the individual bits of integer operands.

17. Signed vs unsigned: Signed variables can represent both negative and positive values, whereas unsigned variables
represent only non-negative values.

18. Passing arrays to functions: Arrays are passed to functions as pointers to their first elements.

19. exit() vs return in main(): The exit() function terminates program execution immediately, while return exits the main
function and returns control to the operating system.

20. Linked list: A linked list is a linear data structure consisting of nodes where each node contains data and a pointer to the
next node.

21. Shallow vs deep copy: A shallow copy duplicates the references to data, whereas a deep copy duplicates the actual data
itself.
22. Typecasting: Typecasting is the explicit conversion of a variable from one data type to another.

23. Preventing buffer overflow: Buffer overflow can be prevented by validating input sizes and using secure input functions.

24. fgets() vs gets(): fgets() reads a specified number of characters and prevents buffer overflow, whereas gets() is unsafe
and deprecated.

25. typedef with pointers and structs: The typedef keyword is used to create aliases for complex pointer and structure types
to enhance code readability.

26. Pre-increment vs post-increment: Pre-increment increments a variable’s value before it is used, while post-increment
uses the variable’s value before incrementing it.

27. Command line arguments: Command line arguments are parameters passed to the program via the main function’s
arguments argc and argv.

28. Memory leak and Valgrind: A memory leak occurs when allocated memory is not properly freed, and Valgrind is a tool
used to detect such leaks.

29. Inline functions: Inline functions suggest to the compiler to replace function calls with the function’s code to reduce
overhead.

30. Implicit vs explicit conversion: Implicit conversion is automatically performed by the compiler, while explicit conversion
requires a cast operator.

You might also like