Advanced topics
1. Preprocessor Directives
Preprocessor directives are instructions that are processed before the actual compilation of the code begins. They start with the # symbol and are used for tasks like including files or defining constants.
#define
The #define directive defines a constant or macro. It helps make the code more readable and easier to maintain.
Syntax:
#define NAME valueExample:
#include <stdio.h>
#define PI 3.14159 // Define a constant for PI
int main() {
printf("Value of PI: %.5f\n", PI); // Use the defined constant
return 0;
}#define PI 3.14159: DefinesPIas3.14159.This allows us to use
PIin our code instead of writing the number directly each time.
#include
The #include directive is used to include the contents of one file within another file. It’s commonly used to include standard library headers or other user-defined files.
Syntax:
Example:
#include <stdio.h>: Includes the standard input/output library.#include "myheader.h": Includes a custom header file namedmyheader.h.
2. Command Line Arguments
Command line arguments are used to pass additional information to a program when it is executed. They provide a way to control the behavior of the program from the command line.
Syntax:
Example:
argc: Counts how many arguments were passed to the program.argv: An array of strings, where each string is one of the command line arguments.
Running the Program:
3. Linked Lists
A linked list is a data structure where each element (node) points to the next one, forming a chain. This allows for flexible memory usage and easy insertion and deletion of elements.
Basic Linked List Node
Definition:
Example:
Node Structure: Contains
dataand a pointer to the next node.Traversal: Moving from the
headnode through eachnextnode until reachingNULL.
4. Stacks
A stack is a data structure that follows the Last In, First Out (LIFO) principle. It has two main operations: push (add an item) and pop (remove an item).
Stack Implementation Using Linked List
Example:
Push: Adds a new item to the top of the stack.
Pop: Removes and returns the item from the top of the stack.
5. Queues
A queue is a data structure that follows the First In, First Out (FIFO) principle. It has two main operations: enqueue (add an item) and dequeue (remove an item).
Queue Implementation Using Linked List
Example:
Enqueue: Adds a new item to the rear of the queue.
Dequeue: Removes and returns the item from the front of the queue.
Summary
Preprocessor Directives:
#definefor constants,#includefor file inclusion.Command Line Arguments: Access program arguments from the command line.
Linked Lists: Dynamic data structures with nodes linked together.
Stacks: LIFO structure with push and pop operations.
Queues: FIFO structure with enqueue and dequeue operations.
Last updated
Was this helpful?