Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [c]

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.

139
votes
17answers
26k 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 ...
128
votes
17answers
16k views

Disproving Euler proposition by brute force in C

I wrote a little bit of code to brute force disprove the Euler proposition that states: $$a^4 + b^4 + c^4 = d^4$$ has no solution when \$a\$, \$b\$, \$c\$, \$d\$ are positive integers. I'm no ...
42
votes
5answers
6k 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 ...
38
votes
3answers
8k views

Why is my C program for calculating Euler's constant of poor quality?

I was trying to post this code to a Wikipedia article, but it was soon removed. I then asked about the code in the page's talk section, and some other contributors said that is was "very poor" and ...
37
votes
5answers
3k views

Running Lights - Embedded “Hello World”

I'm getting my feet wet in embedded development, and like all those before me, my first task was to make an LED blink. I went a bit further than that and made a "runner" that lights up each led from 0 ...
37
votes
4answers
6k views

Ultimate Tic-Tac-Toe in C

Here is my attempt at the UTTT code-challenge (in response to the the Weekend-Challenge Reboot). Here is what I would like critiqued: I tested the code a few times for bugs, but I may have missed ...
36
votes
5answers
11k views

Event scheduler in C

One of my university assignments asked us to create a program using struct in order to create a simple event scheduler. This program is for a single day only, not ...
36
votes
2answers
58k 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 ...
36
votes
9answers
11k views

Copying 80 bytes as fast as possible

I am running a math-oriented computation that spends a significant amount of its time doing memcpy, always copying 80 bytes from one location to the next, an array ...
35
votes
8answers
8k views

C program that reverses a string

As practice with the (ever so painful) C Strings I wrote a string reversal program. I am new to C, so I wanted to make sure that I am not using bad coding practices. It runs just fine (at least with ...
34
votes
5answers
5k views

Recording Audio Continuously in C

As an ongoing little side project of mine, I've been working on recording audio in c. You can the code's progression by looking at past versions (V1.0, V2.0). I've found that my past versions were ...
30
votes
7answers
8k views

Checking if a number is divisible by 9

I tried to develop a new way to check if a number is divisible by 9. I have written my code and it is working fine. I'm not allowed to use *,...
30
votes
4answers
14k views

Recording audio in C

Please note there are newer revisions of this code, one here, and one here for continuous audio recording. This is a program I wrote as a .wav audio recording library for Linux. It was developed on ...
29
votes
5answers
43k views

Brute Force Algorithm in C

This is a simple brute force algorithm that I have programmed in C. All the program does is print out every possible combination of the given alphabet for the ...
29
votes
6answers
3k 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 on ...
27
votes
4answers
6k views

Loading… animating dots in C

I've recently wanted to make a "Loading..." display in C where the dots print one at a time in order and then reset: Suprisingly, there isn't much on the internet for doing this well, so I figured I ...
27
votes
5answers
20k views

Tiny Encryption Algorithm (TEA) for arbitrary sized data

The TEA is a very simple encryption algorithm requiring little time and space - perfect for embedded systems. There are extensions to it, and every version has its flaws (WEP was based on it), but ...
26
votes
3answers
5k views

Message decoder

I've built a C decoder program. The length of an encoded message will be given and the message itself in the following line. All the characters of the message will be in uppercase letters. The task is ...
26
votes
1answer
7k views

SIMD matrix multiplication

I recently started toying with SIMD and came up with the following code for matrix multiplication. First I attempted to implement it using SIMD the same way I did in SISD, just using SIMD for things ...
25
votes
8answers
35k views

Using a for-loop or sleeping to wait for short intervals of time

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 ...
25
votes
4answers
48k views

Counting number of 1's and 0's from integer with bitwise operation

Is there a better way of doing this? ...
24
votes
6answers
5k views

Create a C style char** from a C++ vector<string>

I am dealing with some older C style APIs, like Posix execve that take a char**. In the rest of my code, I prefer to use a ...
24
votes
4answers
5k views

6 out of 49 Lottery

In Germany we have a lottery with the following rules: You have to guess 6 numbers. The numbers shall be less than 50 and greater then 0. A number occurs only one time in a game. So you can not guess ...
24
votes
3answers
2k views

“The python that ate a calculator”

In a YouTube video I watched, the speaker said it is important to know how to calculate between numbers of different bases; i.e. two, ten, and sixteen. I was then inspired to write a small C program ...
23
votes
4answers
3k views

Project Euler #1 Sum of all the multiples of 3 or 5 below 1000

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. My ...
23
votes
3answers
2k views

2048 with GUI in C

0x2048 This is my implementation of the classic game "2048" in C. The instruction to build/run the project (GitHub repo) can be found here. I started with the core game and then built a GUI ...
22
votes
5answers
8k views

Implementing realloc in C

I have an assignment to implement realloc in C, This's my code please review it. ...
22
votes
8answers
4k views

Feedback on a programming practice problem in C

This is a programming practice that our teacher gave us, and I would appreciate if someone can look over my program and tell me if I did a good job. Basically, the context is I joined a company and ...
22
votes
6answers
4k views

Tic-Tac-Toe program in about 250 lines

I'm new to C programming and I recently challenged myself to writing a Tic-Tac-Toe program. I found that my code becomes lengthy very quickly with so many if ...
22
votes
4answers
4k views

Generating 1000 random numbers

I have got the following C code and I feel it is crazy stupid and there must be a better way to do this, but just can't think of one and have yet to learn algorithms. ...
22
votes
2answers
3k views

First prime number larger than given integer

How can I test this C program for "efficiency"? The most interesting usage is that it returns negative output for large enough input, otherwise the behavior is about expected. Will you suggest how to ...
22
votes
4answers
46k views

Simpler method to detect int overflow

To detect int overflow/underflow in C, I use this code. What might be a simpler and more portable way of coding this (that is, fewer tests)? Assume 2's complement ...
22
votes
2answers
30k views

Windows keylogger in C

I have had this keylogger code for a while now (a few years*), and I figured I would put it up for review. Here is what I would like reviewed (in order): Portability - right now, this program can ...
22
votes
2answers
1k 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 ...
22
votes
4answers
4k views

Fast 32x32 bit multiply on ARM M0

I have a time-critical calculation running on an ARM Cortex-M0. This is the fastest 32x32 bit -> 64 bit multiplication that I can come up with. The following C compiles almost 1-1 to assembly ...
22
votes
4answers
10k views

Simple Turing machine simulator

Yesterday I got a sudden urge to go from writing Python to something, well, more UNIX core-ish. I read some examples on here and decided I might as well put some of that stuff to use to test something ...
21
votes
5answers
2k views

Word Inverter in C

I've just made this program and I want to know how to improve it. It just receives a word and prints it backwards. Sequences of numbers are also accepted, but not white spaces (that's why I used scanf)...
21
votes
1answer
1k views

Tiny Lua library to get char pointer from string

Background I'm using Lua with luaglut to do some OpenGL stuff. The luaglut API is almost identical to the gl/glut C APIs. Sometimes, gl functions want a pointer to some data, for example: ...
20
votes
10answers
5k 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, right?)...
20
votes
7answers
5k views

Finding pairs of Amicable Numbers, up to 10000

I have this program to find pairs of amicable numbers in C: ...
20
votes
5answers
3k views

Setting and getting bits in C

I am just starting to get my feet wet with C; and am rather enjoying myself in doing so. This is sort of a follow up to my last question here. I am posting this ordinate to hopefully getting some ...
20
votes
3answers
13k views

Calculate the number of moves requires for a knight chess piece [closed]

It's possible to work out the number of moves required to move from (0, 0) to (A, B) for a knight on an infinite chess board in O(1) time. This is an attempt at a solution to do it in O(n) time, ...
19
votes
3answers
4k views

Credit card verification: string conversion most optimal?

Below is some code which verifies a credit card number using the checksum as well as check if number of digits are appropriate as well if digits start with right numbers. I am not sure if converting ...
19
votes
1answer
653 views

Spell Corrector in C

I recently stumbled across this article on how to write a spelling corrector, and figured I'd try to have a go at it in C (mainly because the link at the end of the page for the C code is broken). ...
19
votes
3answers
2k views

Simple webserver in C

My first learning project in C. Looking for general feedback e.g. gotchas, coding standards, formatting, naming etc. I'm here to learn! webserver.h ...
19
votes
1answer
524 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! ...
19
votes
1answer
343 views

Suggesting “safe enum” class for ISO C

There are quite some questions and answers about how to make enum really type-safe and I didn't find a solution that ensures both type safety and valid values. So I ...
18
votes
7answers
671 views

Converting inches to feet

This was a homework assignment that I'm now done with - I submitted it as is. However the fact that I needed to use the same code twice bugged me... The double code is: ...
18
votes
5answers
5k views

Program that removes comments from C and C++ programs

I have downloaded the latest version of the Linux kernel and I want to perform thorough analysis on it. I want to start by eliminating comments from all the source and header files that belong to it. ...
18
votes
4answers
2k views

Basic Word Counter in C

Working on a C programming question where I have to write a function which will determine how many words are in a given string. Assume that one or more consecutive white spaces is a delimiter between ...