Most viewed C recipes http://code.activestate.com/recipes/langs/c/views/2015-10-16T12:11:58-07:00ActiveState Code RecipesImplementation of Knuth–Morris–Pratt algorithm (C) 2011-10-15T00:33:41-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577908-implementation-of-knuthmorrispratt-algorithm/ <p style="color: grey"> C recipe 577908 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/kmp/">kmp</a>, <a href="/recipes/tags/matching/">matching</a>, <a href="/recipes/tags/string/">string</a>). Revision 2. </p> <p>Knuth–Morris–Pratt (KMP) is a linear time string matching algorithm.</p> <p><a href="http://en.wikipedia.org/wiki/Knuth%25E2%2580%2593Morris%25E2%2580%2593Pratt_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm</a></p> <p>This recipe gives a C-version implementation.</p> Setting a file descriptor to blocking or non-blocking mode (C) 2010-09-06T11:20:53-07:00Emilio Montihttp://code.activestate.com/recipes/users/4173642/http://code.activestate.com/recipes/577384-setting-a-file-descriptor-to-blocking-or-non-block/ <p style="color: grey"> C recipe 577384 by <a href="/recipes/users/4173642/">Emilio Monti</a> (<a href="/recipes/tags/blocking/">blocking</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/non_blocking/">non_blocking</a>). Revision 2. </p> <p>A simple function to set a file descriptor (i.e. a socket) to blocking or non-blocking mode.</p> redirect printf, stdout,stderr (C) 2009-07-02T00:36:46-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576825-redirect-printf-stdoutstderr/ <p style="color: grey"> C recipe 576825 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/printf/">printf</a>, <a href="/recipes/tags/redirect/">redirect</a>). </p> <p>redirect printf, stdout,stderr</p> Wav audio file dynamic range compressor (C) 2013-04-18T15:04:25-07:00Granning Stolinehttp://code.activestate.com/recipes/users/4186069/http://code.activestate.com/recipes/578510-wav-audio-file-dynamic-range-compressor/ <p style="color: grey"> C recipe 578510 by <a href="/recipes/users/4186069/">Granning Stoline</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/compressor/">compressor</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/wav/">wav</a>). Revision 4. </p> <p>Wav audio file dynamic range compressor</p> Random Number Game (C) 2012-03-29T03:47:52-07:00Jacob A. Bridgeshttp://code.activestate.com/recipes/users/4181498/http://code.activestate.com/recipes/578088-random-number-game/ <p style="color: grey"> C recipe 578088 by <a href="/recipes/users/4181498/">Jacob A. Bridges</a> (<a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/guessing/">guessing</a>, <a href="/recipes/tags/number/">number</a>). </p> <p>An old game I created freshman year of college. For use on Windows OS only. (The code contains a few Windows system calls.)</p> csc prototype to arduino board with python API. part I (C) 2010-12-26T12:14:33-08:00cheeng shu chinhttp://code.activestate.com/recipes/users/163970/http://code.activestate.com/recipes/577520-csc-prototype-to-arduino-board-with-python-api-par/ <p style="color: grey"> C recipe 577520 by <a href="/recipes/users/163970/">cheeng shu chin</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/arduino/">arduino</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/serial/">serial</a>). Revision 2. </p> <p>Few day ago, i'm try out arduino UNO robotic board. found it not that easy to use (C &lt;--> Python). I wrote a arduino UNO prototype to serial API, which can interface to any programming Language. as long as the programming Language can interface to virtual serial port and using serial API, this recipe can be use... Requirement:</p> <p>Upload this recipes to arduino uno with my prototype code (name "csc.pde" as below)</p> <p>Base concept:</p> <ol> <li>wait from serial reply "?" and ready for read</li> <li>pass function name as string to arduino uno</li> <li>pass all argument as string to arduino uno</li> <li>read result as string from arduino uno</li> </ol> <p>Can easy extend it to support:</p> <ol> <li>python API(open source and i like most) in part II</li> <li>other arduino board</li> <li>Bluetooth with serial interface</li> <li>any PC can control the arduino board easily via your prefer Language.</li> <li>interrupt </li> </ol> <p>Future add-on:</p> <ol> <li>Json string passing to arduino</li> <li>Json Reply from arduino</li> <li>thread base design</li> <li>Interrupt direct call</li> </ol> <p>Please study it and extend it and share among open source members especially in python... :)</p> <p>Next <a href="http://code.activestate.com/recipes/577521-csc-prototype-to-arduino-board-with-python-api-par/">Part II</a></p> pending/blocking a signal (C) 2009-05-25T22:42:30-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576775-pendingblocking-a-signal/ <p style="color: grey"> C recipe 576775 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/signal/">signal</a>). </p> <p><a href="http://www-h.eng.cam.ac.uk/help/tpl/unix/signals.html" rel="nofollow">http://www-h.eng.cam.ac.uk/help/tpl/unix/signals.html</a></p> <p>do you want certain signals to be ignored or blocked? The sigaction(), sigprocmask(), siginterrupt(), and sigsuspend() functions control the manipulation of the signal mask, which defines the set of signals currently blocked. The manual pages give details. The following code shows how the response to signals can be delayed. </p> create a file and its parents (C) 2009-05-20T18:15:55-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576770-create-a-file-and-its-parents/ <p style="color: grey"> C recipe 576770 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/file/">file</a>). </p> <p>walking down the path, create the subpath when necessary</p> escape url characters (C) 2009-09-28T20:41:05-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576916-escape-url-characters/ <p style="color: grey"> C recipe 576916 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/string/">string</a>). </p> <p>对于url中的一些特殊字符会被转义以利传输, * 并且, 归档服务器上, php写入cookie中的值可能存在特殊字符, 需要转换</p> null null (C) 2013-05-26T10:59:13-07:00Joe Smithhttp://code.activestate.com/recipes/users/4168055/http://code.activestate.com/recipes/577019-null-null/ <p style="color: grey"> C recipe 577019 by <a href="/recipes/users/4168055/">Joe Smith</a> (<a href="/recipes/tags/null/">null</a>). Revision 2. </p> <p>null null</p> Free segmentation fault caused by buffer overflow (C) 2009-10-10T02:45:48-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576927-free-segmentation-fault-caused-by-buffer-overflow/ <p style="color: grey"> C recipe 576927 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/bufer_overflow/">bufer_overflow</a>). Revision 2. </p> <p>Free segmentation fault caused by buffer overflow</p> commanding via a duplex pipe stream. (C) 2009-06-07T01:41:19-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576773-commanding-via-a-duplex-pipe-stream/ <p style="color: grey"> C recipe 576773 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/pipe/">pipe</a>). Revision 2. </p> <p>commanding via a duplex pipe stream.</p> sigaction on SIGCHLD (C) 2009-05-25T23:54:57-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576776-sigaction-on-sigchld/ <p style="color: grey"> C recipe 576776 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/signal/">signal</a>). Revision 2. </p> <p>sigaction</p> the list template (C) 2009-07-30T22:59:20-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576864-the-list-template/ <p style="color: grey"> C recipe 576864 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/list/">list</a>). </p> <p>reference: <a href="http://www.makelinux.net/ldd3/chp-11-sect-5.shtml" rel="nofollow">http://www.makelinux.net/ldd3/chp-11-sect-5.shtml</a></p> Self-Printing Program (C) 2009-06-05T01:19:17-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576798-self-printing-program/ <p style="color: grey"> C recipe 576798 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/tricky/">tricky</a>). </p> <p>The two key tricks here are using a string with an embedded %s specifier to allow the string to contain itself when printed, and to use the %c format specifier to allow printing out special characters like newlines, which could not otherwise be embedded in the output string. </p> Small random number generator (C) 2012-05-15T19:49:34-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578134-small-random-number-generator/ <p style="color: grey"> C recipe 578134 by <a href="/recipes/users/4181845/">Sachin Joglekar</a> (<a href="/recipes/tags/generate/">generate</a>, <a href="/recipes/tags/malloc/">malloc</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/stdlib/">stdlib</a>). </p> <p>Generates random numbers in a given range using malloc function in stdlib.h. Based on the observation that the memory address allocated during malloc is usually 'random' (for humans). takes in two arguments- start of range, end of range.</p> make subdirs (C) 2009-06-16T19:47:31-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576813-make-subdirs/ <p style="color: grey"> C recipe 576813 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/mkdir/">mkdir</a>). </p> <p>make subdirs recursively</p> Add function to Python's __builtin__ module through C API (C) 2015-10-16T12:11:58-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579110-add-function-to-pythons-__builtin__-module-through/ <p style="color: grey"> C recipe 579110 by <a href="/recipes/users/4192997/">airween</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/builtins/">builtins</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/exten/">exten</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Add function to __builtin__ module through C API</p> <p>Sometimes it need to embedding a Python script to a C code, and it references to a function, which also provided by the same C code. Then you have to import the module, as you defined in your C code.</p> <p>But this import would be skipped, if you add your function to your __builtin__ module. In Python3 (3.5), there is the PyModule_AddFunctions() function, but in the previous versions, you can make it like this snippet.</p> <p>See these recipes:</p> <p>Makefile: <a href="https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/</a></p> <p>Python script: <a href="https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/</a></p> code snippets (C) 2009-08-17T18:10:19-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576856-code-snippets/ <p style="color: grey"> C recipe 576856 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/snippet/">snippet</a>, <a href="/recipes/tags/struct_tm/">struct_tm</a>). Revision 3. </p> <p>code snippets</p> getcwd (C) 2009-07-23T20:01:42-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576855-getcwd/ <p style="color: grey"> C recipe 576855 by <a href="/recipes/users/4170398/">J Y</a> (<a href="/recipes/tags/getcwd/">getcwd</a>). </p> <p>getcwd</p>