C
C
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.
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')
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.
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.
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.
typedef in C is a keyword used to create new names (aliases) for existing data types, making code easier to read and
manage.
Loops are control structures that allow you to execute a block of code
• 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:
Functions are blocks of code designed to perform a specific task, which can be called multiple times throughout a
program.
Types of functions:
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.
A structure (struct) is a user-defined data type that groups variables of different types under a single name.
Example:
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.
• 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)
• 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
sleep(seconds) pauses program execution for a specified number of seconds (available in <unistd.h> on Unix/Linux).
Example:
25. What is the volatile keyword?
volatile tells the compiler that a variable’s value may change unexpectedly, preventing optimization on that variable.
• Object code: Machine code generated by compiler, not human-readable (.obj or .o files)
Passing the address of variables to a function so it can modify the original variables directly.
Memory leak occurs when dynamically allocated memory is not freed, causing wasted memory.
Avoid it by: Always using free() for allocated memory once done.
Parameters passed to the program at startup via main(int argc, char *argv[]).
auto declares automatic local variables (default storage class inside functions); usually omitted as it’s default.
extern declares a variable defined in another file, allowing access across multiple files.
• Format specifiers indicate data type, e.g., %d (int), %f (float), %c (char), %s (string).
• Far pointer: 32-bit pointer with segment:offset, can access beyond current segment
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.