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 value

Example:

#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: Defines PI as 3.14159.

  • This allows us to use PI in 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 named myheader.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 data and a pointer to the next node.

  • Traversal: Moving from the head node through each next node until reaching NULL.


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: #define for constants, #include for 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?