Basic Concepts
Basic Concepts
1. Compiler vs. Interpreter: A compiler translates the entire source code into machine code
before execution, while an interpreter translates code line-by-line during execution.
3. Variables and Constants: Variables are storage locations that can change during execution;
constants are fixed values that cannot change.
4. Header File Purpose: Header files contain declarations for functions and macros, allowing for
modular programming and code reuse.
5. Defining a Macro: Use #define to define a macro. For example: #define PI 3.14.
6. Data Types in C: Common types include int, float, double, char, and derived types like arrays
and structures.
7. Signed vs. Unsigned Integers: Signed integers can represent both positive and negative
values, while unsigned integers only represent non-negative values.
8. Float vs. Double: float has single precision (usually 32 bits), while double has double
precision (usually 64 bits).
10. Precedence and Associativity: Operator precedence determines the order of operations;
associativity determines the direction of evaluation (left-to-right or right-to-left).
Control Structures
Copy code
if (condition) {
// code if true
} else {
// code if false
12. Switch Statement: A switch statement evaluates an expression and executes code blocks
based on matching case labels.
13. While vs. Do-While: A while loop checks the condition before executing the loop body; a do-
while loop executes the body at least once before checking the condition.
Copy code
// loop body
15. Break and Continue: break exits the loop, while continue skips to the next iteration of the
loop.
Functions
16. Functions: Functions are blocks of code that perform specific tasks and can be reused
throughout the program.
17. Call by Value vs. Call by Reference: Call by value passes a copy of the variable; call by
reference passes a reference (pointer) to the variable.
Copy code
int factorial(int n) {
19. Passing Arrays: Arrays are passed to functions as pointers. Example: void func(int arr[]).
20. Purpose of void: Used to indicate a function does not return a value.
Arrays
21. Arrays in C: Arrays are collections of elements of the same type. They are declared with a
type and size, e.g., int arr[10];.
22. Initializing an Array: You can initialize during declaration: int arr[3] = {1, 2, 3};.
23. Passing an Array: Arrays are passed by reference; use the array name as a parameter.
24. Matrix Multiplication: Implement using nested loops to multiply corresponding elements.
25. Searching in an Array: Use a loop to iterate through the array to find the element.
Pointers
26. Pointer: A pointer is a variable that stores the memory address of another variable.
28. Pointer Arithmetic: You can perform arithmetic operations (e.g., ptr++) to navigate through
memory.
29. Passing a Pointer: Similar to passing an array, you can pass a pointer to a function to modify
the original variable.
30. Pointer vs. Array: A pointer holds an address, while an array is a fixed-size collection of
elements.
Strings
31. Declaring a String: A string can be declared as a character array: char str[10];.
32. String Functions: Functions like strcpy and strcmp manipulate strings by copying and
comparing them, respectively.
33. Reversing a String: Use a loop to swap characters from both ends towards the center.
34. Checking Palindrome: Compare characters from both ends towards the center.
35. String vs. Character Array: A string is a character array terminated by a null character (\0).
36. Structure: A structure is a user-defined data type that groups related variables.
Copy code
struct Person {
char name[50];
int age;
};
38. Structure vs. Union: Structures allocate memory for all members, while unions share the
same memory location among members.
39. Initializing a Structure: You can initialize with an aggregate initializer: struct Person p =
{"Alice", 30};.
File Handling
41. Opening a File: Use fopen(), e.g., FILE *fp = fopen("file.txt", "r");.
42. File Modes: Common modes include "r" (read), "w" (write), "a" (append).
45. fgetc() and fputc(): These functions read and write a single character from/to a file.
47. malloc() and calloc(): malloc() allocates uninitialized memory; calloc() allocates initialized
memory.
50. Memory Leaks: Occur when allocated memory is not freed. Prevent by always calling free().
51. Preprocessors: They process directives before compilation (e.g., #include, #define).
53. Conditional Compilation: Use #ifdef, #ifndef, etc., to include/exclude code based on
conditions.
54. Macros: Defined with #define; they replace code before compilation.
55. Macros vs. Functions: Macros are expanded inline, while functions have overhead due to
calls.
Storage Classes
57. Auto vs. Static: auto is the default storage class with block scope; static retains value across
function calls.
59. Register Storage Class: Suggests that the variable be stored in CPU registers for faster access.
60. Scope and Lifetime: Varies by storage class, affecting visibility and duration of variable
existence.
Linked Lists
61. Linked List: A data structure consisting of nodes where each node points to the next.
62. Singly Linked List Implementation: Consists of a struct for nodes with a value and a pointer
to the next node.
63. Array vs. Linked List: Arrays have fixed size and random access; linked lists are dynamic and
use sequential access.
65. Deleting a Node: Adjust pointers to exclude the node from the list.
Miscellaneous
66. Segmentation Fault: Occurs when accessing memory that is not allocated or allowed.
70. Command-Line Arguments: Arguments passed to the program via the command line,
accessible via argc (argument count) and argv (argument vector).