C and C++ Source Code Snippets

Welcome to Cprogramming.com's programming code snippet repository. Peruse the archives or add your own snippets for others to use! (Check the following link a better definition of a snippet)

Sort by clicking on any of the headers.

SnippetAuthorLanguagePlatformRating (votes)
using struct tm, time_t, and time to create a simple clockDieter Manstein CANSI5.3 (50)
A singly linked list with a sentinel implementationyzb3 CANSI5.6 (63)
Useful string functionsyzb3 CANSI6.0 (217)
string to bitstring, bitstring to stringEsa Karjalainen CANSI5.4 (87)
Initial values for static multidimensional arrayAli Nawkhas Murad C or C++ANSI5.2 (144)
Another way to swap 2 numbersMaganti Venkatesh C or C++ANSI5.4 (100)
Simple swapping heapsortyzb3 C or C++ANSI5.3 (195)
Find the number of CPU Cores, for Windows, Mac or LinuxDirk-Jan Kroon C or C++Multiple6.5 (150)
Find the minimum distance between two linesGiacomo C or C++ANSI4.3 (78)
Merge sort of an integer listBibhubrata Narendra C or C++ANSI4.6 (79)
Generate a spiral matrix for n elementsRaza C or C++ANSI5.7 (145)
This C code assigns codes to a maximum of 10 charsAbhishek H.Dwivedi C or C++Windows4.6 (74)
This C code recursively sorts an array of upto 20Abhishek H.Dwivedi C or C++ANSI5.4 (273)
This C code uses the merge sort algorithm to sortAbhishek H.Dwivedi C or C++ANSI5.5 (710)
A game setup for DirectXPaul D C or C++ANSI6.1 (405)
A function to reverse an integer arrayMajor_Small C or C++ANSI5.9 (599)
Checking for powers of twoWebmaster C or C++ANSI5.8 (381)
a short BubbleSort algoritm for Integer ArraysMajor_Small C or C++ANSI6.2 (328)
A code template for OpenGL / GLUTPerspective C or C++ANSI6.9 (835)
Rectangle Collision DetectionDavid Nielsen C or C++ANSI5.7 (249)
Converts a real number to the string.Darko Kolakoviћ C or C++ANSI5.8 (297)
Validate Longitudinal Redundancy Checksum (LRC)Darko Kolaković C or C++ANSI5.4 (216)
One line swap of integersabhilash C or C++ANSI5.6 (249)
Convert NTSTATUS code to Win32 errorIan Abbott C or C++Windows5.4 (147)
Macro to swap nibble of BYTET Sailesh C or C++ANSI5.4 (98)
binary tree creation, traversal and searchSyed Rafey Husain C or C++ANSI6.3 (301)
Knuth-Morris-Pratt (KMP) String Search AlgorithmSyed Rafey Husain C or C++ANSI6.6 (933)
Count number of bits in a numberNaren Bala C or C++ANSI5.9 (60)
Three ways to do a swapDavid Nielsen C or C++ANSI5.6 (415)
euler approximation methoderic mbakop C or C++ANSI5.6 (329)
Next 30
Previous 30