C is a general-purpose computer programming language used for operating systems, games, and other high performance work and is clearly distinct from C++. It was developed in 1972 by Dennis Ritchie for use with the Unix operating system.

learn more… | top users | synonyms

26
votes
5answers
18k views

Brute Force Algorithm in C

This is a simple brute force algorithm I have in C. All the program does it print out every possible combination of the given alphabet for the given length. I ...
29
votes
2answers
31k views

Sudoku Solver in C

I had this code lying around, so I figured I would submit this as my first attempt at a weekend-challenge. I would prefer if reviews contained suggestions on how to improve the algorithm, but all ...
16
votes
7answers
9k views

Using a for-loop instead of sleeping?

I need to wait for short intervals of time in my program, but I really don't want to include unistd.h just to use sleep, so I'm ...
10
votes
4answers
602 views

Algorithm for simple string compression

I attempted a problem from the Cracking the Coding Interview book. The following input: aabcccaaa should give the following output: ...
10
votes
3answers
1k views

Coin Change: Minimum number of coins

Problem: You are given n types of coin denominations of values \$v(1) < v(2) < ... < v(n)\$ (all integers). Assume \$v(1) = 1\$, so you can always make change for any amount of money ...
1
vote
1answer
59 views

A trivial command line utility for trimming whitespace from lines in C - follow-up 2

The previous iteration at A trivial command line utility for trimming whitespace from lines in C - follow-up Note: the next iteration at A trivial command line utility for trimming whitespace from ...
17
votes
2answers
683 views

parcel: a JSON parsing library in C

I've created a JSON parsing library in C, and would like some feedback on it (feel free to submit a pull request on GitHub). Any and all suggestions are acceptable, but I would prefer if reviews were ...
9
votes
1answer
444 views

ToyVM - a small and simple virtual machine in C - follow-up

(See the previous and initial iteration) Now I have refactored the code in order to conform to the suggestions made in the answers to the first iteration. I have this: toyvm.h: ...
14
votes
7answers
50k views

Euclid's Algorithm (Greatest Common Divisor)

According to Donald Knuth in "The Art Of Computer Programming", the following is how to find the greatest common divisor of two positive numbers, m and n, using Euclid's Algorithm. Divide m by ...
25
votes
4answers
6k views

Recording audio in C

Please note that there are newer revisions of the code in this question, one here as a newer version of this code, and one here for continuous audio recording. This is a program I wrote as a .wav ...
12
votes
2answers
2k views

More efficient way to retrieve data from JSON

I am using the jsmn JSON parser (source code) to get extract data from a JSON string. jsmn stores the data in tokens that just point to the token boundaries in the JSON string instead of copying the ...
10
votes
4answers
279 views

Writing computer generated music to a .wav file in C - follow-up

See the previous and initial iteration. See the next iteration. Now I have refactored the code according to many suggestions made in the first iteration. What's new: I attempted to deal with ...
7
votes
2answers
225 views

png2jpeg, a utility for converting PNG to JPEG (rev. 1/3)

Revisions: Revision 1 (you are here) Revision 2 Revision 3 I've been working on a simple command-line png2jpeg utility. Its purpose is to convert images from PNG format to JPEG. The source code ...
2
votes
2answers
124 views

png2jpeg, a utility for converting PNG to JPEG (rev. 2/3)

Revisions: Revision 1 Revision 2 (you are here) Revision 3 png2jpeg is a simple command-line converter of PNG to JPEG. The source code requires the libpng and libjpeg development libraries to be ...
8
votes
2answers
212 views

Simple compression reloaded++

As a follow up to my previous question I've improved the code + algorithm. The compression now works the following way: Each character is followed by a length byte. The top 3 bits of that byte denote ...
5
votes
5answers
2k views

String traversal program in C

My task was to write a function in C where a string such as {2210090,34566,87234,564676} would be given as input and the function had to find out the count of ...
5
votes
5answers
928 views

Finding the maximum row of a 4x4 matrix

I have written a recursive function that I believe will return the 'maximum' row of the 4x4 matrix it is fed. By 'maximum' I mean that the maximum row of the matrix. ...
4
votes
1answer
55 views

Simple spinlock for C using ASM

This is my second attempt to make simple lock using extended assembly. See http://stackoverflow.com/questions/37241553/locks-around-memory-manipulation-via-inline-assembly/37246263 The code: ...
4
votes
3answers
276 views

Implementing N-ary trees in C

Looking at my code, I feel like it is not instantly clear for someone who did not write it. I feel like readability will be important for me in the future, if I'm ever to write code on an ...
4
votes
1answer
172 views

UTF-8 encoding/decoding

I have two functions, a collection of possible error codes, and a unit-testing framework. The parsing of a character into its unary prefix and payload is handled by a few named functions and macros ...
4
votes
1answer
109 views

Synchronous events library

I have implemented a small library that handles synchronous events with POSIX compliant threads. I oriented me on the already existing POSIX thread API. Here are the files I created: ...
3
votes
5answers
82 views

Toggle four relays on/off based on bitmap stored in uint8_t

I am using a one-byte bitmap to control four relays. I am writing a function which will take the bitmap stored as uint8_t, look to see which bits are set/cleared, ...
3
votes
1answer
62 views

Testing for divisibility of numbers - follow-up

(Here is the follow up question) I had another go at trying to speed up my program described in my previous question. JS1's answer was particulary helpful and now my code is only about 20% slower ...
3
votes
1answer
66 views

Data Structures in C (Single Linked List) - Follow-up

This is a follow-up from my previous question! I cleaned up the code and added all the notes from the answers. I have also updated the code on github LinkedList.h ...
3
votes
4answers
172 views

Simple calculator in C V2

My last question had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to ...
3
votes
2answers
61 views

A trivial command line utility for trimming whitespace from lines in C - follow-up

See the previous iteration: A trivial command line utility for trimming whitespace from lines in C Note: see the next iteration at A trivial command line utility for trimming whitespace from lines in ...
3
votes
3answers
3k views

String in ANSI C?

I'm trying declare a string in ANSI C and am not sure of the best way to do it. ...
3
votes
1answer
146 views

Is my function failproof?

I am implementing a list structure in C. My current function I am working on is a destructive append function that takes two lists and appends the second one onto the end of the first. Right now, it ...
3
votes
5answers
2k views

Delete each character in one string that matches any character in another

I am trying to solve an exercise in which I am given the following function: ...
2
votes
1answer
48 views

Optimize program to test for divisibility of numbers 3.0

This is a follow up on my previous question. JS1's answer suggested that I should use a precomputed table containing all permutations of the lowest valid number for each number between 1 and MAX. It ...
2
votes
2answers
82 views

Program to test for divisibility of numbers

(EDIT: here is the follow up qusetion) Using this program to test for the smallest number where its permutations or itself is divisible by 10 or less numbers is twice as slow as the fastest program I ...
1
vote
2answers
94 views

Fibonacci heap in C - follow-up

(See the previous iteration.) I have incorporated some points made by ChrisWue and refactored my Fibonacci heap implementation: fibonacci_heap.h ...
106
votes
16answers
14k views

Searching an element in a sorted array

I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic ...
33
votes
5answers
3k views

Writing computer generated music to a .wav file in C

See the next iteration. I wrote this simple C program for writing computer generated music to a WAV file over three years ago. I refactored it a little, but it does not look good to me. Any ...
14
votes
3answers
1k views

C Makefile boilerplate

This is the current Makefile that I use with my C projects. Here is what I would like to be reviewed: Reusability - is this Makefile easy to use for multiple separate projects with minimal ...
26
votes
6answers
2k views

Aliens at the train

I solved this problem on SPOJ: The aliens have arrived to Earth and everything is in harmony, the two races can live together. However, one specific Female Alien does not want to see humans ...
15
votes
5answers
9k views

Sine function in C / C++

Due to software constraints, I cannot use the standard libraries, cmath, algorithm, templates, inline, or boost. I am also using ...
16
votes
1answer
212 views

Coroutines in C

Please have a look at this little coroutines library ccoro: http://sam.nipl.net/code/ccoro I'd appreciate a general code and style review, and your kind comments! ...
14
votes
4answers
733 views

checkmate - C spelling corrector 2.0

Since I posted my first version of the spelling corrector here, I've been working on improving it a little in my free time. I've also gone ahead and put the project up on Github so that others can ...
11
votes
2answers
78 views

Implementation of the ls command with several options - follow-up

I've made vast improvements to my previous ls implementation, including: Added new flags: -g (omit owner listing) ...
5
votes
2answers
501 views

Reading, counting, and processing integers from a text file

The following code reads a .txt file that contain only numbers. Read the first one, create a dynamic array and put the other numbers on it, made some procedures and in the end write a file with the ...
17
votes
10answers
4k views

Palindromes in C

The function tests whether or not the provided string is a palindrome, and the main function just times how quick it is (after all, C's supposed to be quick, ...
14
votes
5answers
876 views

Mandelbrot image generator

I have written a C program to generate a PPM (Portable Pixmap) image of the Mandelbrot set. The program implements many things in C I am fairly unfamiliar with (Structures, error handling, use of new ...
12
votes
1answer
201 views

Compile-time data structure generator

In response to another recent question I mentioned that one mechanism to avoid runtime overhead for creating a data structure was to create it at compile time and use it directly. Since there was ...
12
votes
7answers
4k views

Is this a fast implementation of reversing a string?

I thought of this algorithm today. It seems fast, but how can I know? ...
11
votes
1answer
2k views

LZW decompressor in C

Here is a simple decompressor in C. I'd very much appreciate any advice or comments about the structure/logic of the program, style of the code, or reusability of the features. I'm a real novice and ...
11
votes
5answers
317 views

Summing user input

A small project I recently started. The program must sum all the numbers the user input. You must first specify how many numbers there will be. Sumaster.c ...
10
votes
2answers
737 views

Simple key-value store in C, take 2

Followup to Simple key-value store in C. Response to previous reviews You may want to hide the KVSstore structure Done. Add a comparison function pointer to KVSstore. If this pointer is ...
8
votes
2answers
277 views

Scoring and grading answers against an answer key

This is for a university assignment (don't worry the code is in working order), and while I could hand it in as-is and receive full marks I feel like there is probably a more efficient and better way ...
7
votes
2answers
106 views

ToyVM - a small and simple virtual machine in C + FizzBuzz demonstration

(See also the next iteration.) I have this small virtual machine. What is there: Stack and, thus, recursion. Conditional and unconditional jumps and, thus, choice and iteration. What is not ...