Creating a Simple To-Do List in C


Introduction

A to-do list is a common application that helps users keep track of tasks they need to complete. In this guide, we'll walk you through the process of creating a simple to-do list in C. We'll explain how it works, and provide sample code to illustrate its usage.


Designing the To-Do List

Our simple to-do list will be a console application that allows users to add tasks, mark tasks as completed, and view their to-do list.


Sample Code

Let's explore the sample code for creating the simple to-do list in C:


#include <stdio.h>
#include <stdbool.h>
// Maximum number of tasks
#define MAX_TASKS 10
// Structure to represent a task
struct Task {
char description[100];
bool completed;
};
int main() {
struct Task tasks[MAX_TASKS];
int taskCount = 0;
while (1) {
printf("\n----- To-Do List -----\n");
printf("1. Add Task\n");
printf("2. Mark Task as Completed\n");
printf("3. View To-Do List\n");
printf("4. Exit\n");
int choice;
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (taskCount < MAX_TASKS) {
printf("Enter task description: ");
scanf(" %[^\n]", tasks[taskCount].description);
tasks[taskCount].completed = false;
taskCount++;
} else {
printf("To-Do List is full. Cannot add more tasks.\n");
}
break;
case 2:
printf("Enter task number to mark as completed: ");
int taskNumber;
scanf("%d", &taskNumber);
if (taskNumber >= 1 && taskNumber <= taskCount) {
tasks[taskNumber - 1].completed = true;
printf("Task marked as completed.\n");
} else {
printf("Invalid task number.\n");
}
break;
case 3:
printf("----- To-Do List -----\n");
for (int i = 0; i < taskCount; i++) {
printf("%d. %s - %s\n", i + 1, tasks[i].description, tasks[i].completed ? "Completed" : "Not Completed");
}
break;
case 4:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please enter a number between 1 and 4.\n");
}
}
return 0;
}

Usage

To use the to-do list, follow these steps:

  1. Run the program.
  2. Choose options to add tasks, mark tasks as completed, view the to-do list, or exit.
  3. Follow the prompts to perform the selected action.

Conclusion

Creating a simple to-do list in C is a practical exercise that involves user input, arrays, structures, and decision-making with switch statements. This guide introduced the concept of a to-do list, explained how it works in C, and provided sample code to demonstrate its usage. As you continue your C programming journey, you can enhance this to-do list or create more advanced applications.