├── images └── screenshot.png ├── README.md ├── LICENSE └── circular_buffer.c /images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/charlesdobson/circular-buffer/HEAD/images/screenshot.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Circular Buffer 2 | This is a simple implementation of a circular buffer, built in C and executed as a console application. It allows the user to write values to the buffer and read values from the buffer. 3 | 4 | # Motivation 5 | This was an extension of a simple lab for my Data Structures class. A friend was struggling with the concept of circular buffers, so I wrote up a guide for a basic implementation. 6 | Here's a link to the blog post: 7 | - [How To Implement A Simple Circular Buffer In C](https://medium.com/@charlesdobson/how-to-implement-a-simple-circular-buffer-in-c-34b7e945d30e) 8 | 9 | # Build Status 10 | All basic functionality has been added. 11 | 12 | # Screenshots 13 | ![screenshot](images/screenshot.png) 14 | 15 | # Tech 16 | **Built With** 17 | - C 18 | 19 | # License 20 | [MIT](LICENSE) © Charles Dobson 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Charles Dobson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /circular_buffer.c: -------------------------------------------------------------------------------- 1 | // Name : Charles Dobson 2 | // Date : 26 April 2019 3 | // Description : This program is an implementation of a circular buffer. It 4 | // stores and accesses values in a FIFO manner. It gets user input 5 | // to write new values to the buffer and reads these values by 6 | // printing them to the screen. 7 | 8 | 9 | 10 | // LIBRARIES 11 | #include 12 | #include 13 | 14 | 15 | 16 | // CONSTANTS 17 | #define SIZE_OF_BUFFER 8 // Maximum size of buffer 18 | #define MAX_LENGTH_OF_STRING 81 // Arbitrary number for temporary strings 19 | #define SUCCESS 0 20 | #define FAILURE -1 21 | #define EXIT_LOOP 1 22 | 23 | 24 | 25 | // PROTOTYPES 26 | void displayMainMenu(void); 27 | int getNumber(int* pNumber); 28 | 29 | 30 | 31 | int main(void) { 32 | int circularBuffer[SIZE_OF_BUFFER] = { 0 }; // Empty circular buffer 33 | int optionNumber = 0; // User-input option number 34 | int readIndex = 0; // Index of the read pointer 35 | int writeIndex = 0; // Index of the write pointer 36 | int bufferLength = 0; // Number of values in circular buffer 37 | int loopStatus = 0; // Loop condition variable 38 | 39 | while (loopStatus != EXIT_LOOP) { 40 | displayMainMenu(); 41 | getNumber(&optionNumber); // Get option number from user 42 | 43 | /* ---------------------------- OPTION 1 - WRITE --------------------------- */ 44 | 45 | if (optionNumber == 1) { 46 | // Check if buffer is full 47 | if (bufferLength == SIZE_OF_BUFFER) { 48 | printf("\n Buffer is full!\n\n "); 49 | getchar(); 50 | continue; 51 | } 52 | 53 | printf("\n Please input an integer to store in the array\n\n "); 54 | getNumber(&circularBuffer[writeIndex]); // Write number to address of buffer index 55 | 56 | bufferLength++; // Increase buffer size after writing 57 | writeIndex++; // Increase writeIndex position to prepare for next write 58 | 59 | // If at last index in buffer, set writeIndex back to 0 60 | if (writeIndex == SIZE_OF_BUFFER) { 61 | writeIndex = 0; 62 | } 63 | } 64 | 65 | /* ---------------------------- OPTION 2 - READ ---------------------------- */ 66 | 67 | else if (optionNumber == 2) { 68 | // Check if buffer is empty 69 | if (bufferLength == 0) { 70 | printf("\n Buffer is empty!\n\n "); 71 | getchar(); 72 | continue; 73 | } 74 | 75 | printf("\n The output value is %d\n\n ", circularBuffer[readIndex]); 76 | getchar(); 77 | 78 | bufferLength--; // Decrease buffer size after reading 79 | readIndex++; // Increase readIndex position to prepare for next read 80 | 81 | // If at last index in buffer, set readIndex back to 0 82 | if (readIndex == SIZE_OF_BUFFER) { 83 | readIndex = 0; 84 | } 85 | } 86 | 87 | /* ---------------------------- OPTION 3 - EXIT ---------------------------- */ 88 | 89 | else if (optionNumber == 3) { 90 | printf("\n Thanks for using the circular buffer!\n\n"); 91 | loopStatus = EXIT_LOOP; 92 | continue; 93 | } 94 | 95 | /* ----------------------------- INVALID OPTION ---------------------------- */ 96 | 97 | else { 98 | printf("\n Invalid option number!\n\n "); 99 | getchar(); 100 | } 101 | } 102 | } 103 | 104 | 105 | 106 | // Function : displayMainMenu() 107 | // Description : This function clears the screen and then displays the main menu 108 | // for the program. 109 | // Parameters : N/A 110 | // Returns : N/A 111 | void displayMainMenu(void) { 112 | printf("\n======================= CIRCULAR BUFFER ======================\n\n"); 113 | printf(" Please choose from the following options:\n\n"); 114 | printf(" 1 Input Value\n"); 115 | printf(" 2 Output Value\n"); 116 | printf(" 3 Exit Program\n"); 117 | printf("\n==============================================================\n\n "); 118 | } 119 | 120 | 121 | 122 | // Function : getNumber() 123 | // Description : This function gets a single integer as user input. 124 | // Parameters : int* pNumber: pointer to an int to store the number 125 | // Returns : int SUCCESS: if input was an int 126 | // int FAILURE: if input was not an int 127 | int getNumber(int* pNumber) { 128 | // An array of char to store the string 129 | char userInput[MAX_LENGTH_OF_STRING] = { 0 }; 130 | 131 | // Get user input 132 | fgets(userInput, MAX_LENGTH_OF_STRING, stdin); 133 | 134 | // Parse the user input for an integer and store it in the pNumber parameter 135 | if (sscanf(userInput, "%d", pNumber) != 1) { 136 | return FAILURE; 137 | } 138 | 139 | return SUCCESS; 140 | } --------------------------------------------------------------------------------