Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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.

Filter by
Sorted by
Tagged with
6 votes
1 answer
179 views

Reading a file into lines with mmap vs getline

I required a function to read a file into lines for a text editor, and ended up with two routines. One uses getline(), and the other uses ...
Harith's user avatar
  • 6,810
5 votes
1 answer
581 views

Karatsuba algorithm in C

This is the Karatsuba algorithm I wrote: ...
Talete's user avatar
  • 53
4 votes
2 answers
244 views

Implementing basename, stpcpy, asprintf, vasprintf, strchrnul, and strcasecmp

C23 standarized strdup()/strndup(), but left out stpcpy(). ...
Harith's user avatar
  • 6,810
1 vote
0 answers
25 views

A generic queue that transfers data from interrupt to main program

This code aims to implement a queue, which transfers data from interrupt to main program in a bare metal embedded system. There are two execution points, the main program and the irq handler. Both ...
Jackoo's user avatar
  • 11
2 votes
1 answer
32 views

Finding the Size of a File in a Portable Manner (revision 2)

This is a follow up to Find the Size of a File in a Portable Manner (revision). Changes made: The return value of fseek() and ...
Harith's user avatar
  • 6,810
3 votes
2 answers
491 views

Abstracting away ANSI escape sequences

I have an application that requires ANSI escape sequences, so I have attempted to abstract away the magic strings out of the code (they were previously macros) into a separate translation layer, ...
Harith's user avatar
  • 6,810
4 votes
2 answers
69 views

Find the Size of a File in a Portable Manner (revision)

This is a follow up to Find the Size of a File in a Portable Manner. What's new: The fast version (POSIX stat()) does not change the position indicator of the ...
Harith's user avatar
  • 6,810
5 votes
2 answers
547 views

C standard input escape sequence replacer program

I've been recently trying to learn C from the K&R Book The C Programming Language. Currently I'm working on my skills on program control flow, which includes case statements. To train my skills ...
KhodeNima's user avatar
  • 399
4 votes
1 answer
169 views

Find the Size of a File in a Portable Manner

There's no function in the Standard C Library to determine the size of a file. The POSIX Standard has stat()/fstat() which are ...
Harith's user avatar
  • 6,810
5 votes
1 answer
174 views

Unit Tests for an Arena Allocator

In my last post about the allocator, I received some comments about unit-tests: The code looks correct on the majority of the cases, I will suggest run valgrind or define a good set of unit tests ...
Harith's user avatar
  • 6,810
1 vote
3 answers
104 views

Sign a message with a Bitcoin private key

I am writing code in C, and I need to do various operations with Bitcoin, from generating key pairs, to sign messages to post transactions. I found libraries for C++, but nothing in C that does all ...
capodieci's user avatar
6 votes
1 answer
575 views

x86 assembly program to compute some random function

As a way to refresh my knowledge on assembly, I tried to compile the below C program to x86 assembly by hand. ...
Jimmy Yang's user avatar
9 votes
4 answers
2k views

Implementing a simple byte manipulation encryption

Can you kindly take a look at the code? Any suggestions regarding performance, readability, potential bugs, portability, security measures, etc. are highly appreciated. The code reads a character, a ...
user174174's user avatar
7 votes
1 answer
195 views

A simple ray marcher for the command line

This is my attempt at creating a ray marcher for the command line for fun. I have some prior experience with C programming, although I'm far from being an expert, and little to no experience with ...
Knogger's user avatar
  • 173
6 votes
1 answer
376 views

Tic Tac Toe Basic C implementation

Is my implementation idiomatic? Does it suffer from any undefined behaviour? Any pointers/tips would be greatly appreciated. Particularly, I think that the winner function could be improved. ...
wed1may's user avatar
  • 63
1 vote
1 answer
47 views

Advent of Code 2022 Day 13 Solution in C: Sorting and parsing nested list

Link to Original Problem: Advent of Code 2022 Day 13 Question Summary: The task involves sorting and parsing nested lists, with two parts: Determine pairs in the correct order and calculate the sum ...
fish_brain's user avatar
12 votes
2 answers
1k views

Header-only log-structured database

I'm seeking a code review for the ~2000 lines of code. Makefile and tests are available at https://github.com/torrentg/logdb. I would appreciate feedback on the following aspects: Overall code ...
Gerard Torrent's user avatar
5 votes
1 answer
377 views

Optimizing Perfect Hash Table Implementation in C for Improved Performance

I wrote code that implements perfect hash table, according to its description in the book "Introduction to Algorithms by Thomas H Cormen", but the code does not pass time tests in contest. I ...
user avatar
18 votes
3 answers
3k views

A tic-tac-toe game in C

I am (trying) to learn the C programming language for fun with my father's book "The C Programming Language". I tried writing a tic-tac-toe game in C. I have no professional experience, so ...
Uriel's user avatar
  • 183
8 votes
1 answer
580 views

Command-line parser that takes a command in C

Explanation The reason I post this is that I am worried this code is clever, which is usually a bad sign. The command-line parser considers the following synopsis. ...
Schilive's user avatar
  • 243
6 votes
1 answer
151 views

An Arena/Bump Allocator in C (Follow-up)

This is a follow-up to: An Arena/Bump Allocator in C. LOC: 234. (Not including the sample tests, still quite small and manageable.) The allocator uses C99, but sample tests use C11's ...
Harith's user avatar
  • 6,810
8 votes
4 answers
2k views

An Arena/Bump Allocator in C

An arena allocator is a memory allocation technique used in computer programming where memory is allocated from a pre-allocated block (arena) rather than individually requesting memory from the ...
Harith's user avatar
  • 6,810
4 votes
1 answer
107 views

C99 - Lexer for the Monkey Programming Language from the book "Writing An Interpreter In Go"

Below is a C version of a lexer for the Monkey programming language, originally written in Go, as presented in the book Writing An Interpreter In Go. LOC: 371. (Excluding the tests.) Language: Copy ...
Harith's user avatar
  • 6,810
4 votes
1 answer
96 views

C - Force a macro argument to be a string literal

The requirement is to define a macro which takes a single argument that is a string literal. My first try at it was to surround it with empty string literals (I got this from Modern C): ...
Harith's user avatar
  • 6,810
2 votes
2 answers
657 views

Binary tree deepcopy using the Morris traversal in C

I need to perform a deep copy of a binary tree using the Morris traversal. I think I managed to do it, that is the code below returns what I expect; however, I am not 100% sure I have covered every ...
Slav's user avatar
  • 121
5 votes
2 answers
819 views

A String View Library in C

I was recently working through the PintOS projects and became curious if there was a better way to do some string processing in C. Specifically, instead of strtok_r,...
Alex Lopez's user avatar
4 votes
1 answer
68 views

C - Pattern To Initialize Values From Saved Data -- If It Exists

Background Here is an awkward pattern I've come up with to handle initializing the ID of an MPU for an embedded system based on the fairly unique factory self-test values stored in the MPU registers. (...
David C. Rankin's user avatar
4 votes
2 answers
241 views

This is a simple stopwatch cli application with a few options

My goal of this when I started writing it was simply that it be able to record thousandths of a second in a very performant and accurate way. I'm not sure I have achieved the performance aspect of ...
lull's user avatar
  • 43
2 votes
1 answer
49 views

Benchmarking putchar_many vs puts Performance for the Same Byte

Whilst writing an interpreter for Brainfuck, I had collapsed sequences of .......... to a single instruction OP_PUT (10) (Note ...
Harith's user avatar
  • 6,810
4 votes
0 answers
167 views

tbf - An Optimizing Brainfuck Interpreter and Transpiler in C

Language: The Brainfuck programming language consists of eight commands: ...
Harith's user avatar
  • 6,810
7 votes
3 answers
2k views

Shunting yard calculator in C

I'm new to C, and made this program that parses a string expression into a linked list post-fix expression, and then evaluates it, without parsing the string beforehand. Wondering if there is anything ...
expoodo's user avatar
  • 73
0 votes
1 answer
205 views

Literate Programming in C

Inspired from Literate programming in Haskell. Below follows a Literate script filter. A literate script is a program in which comments are given the leading role, whilst program text must be ...
Harith's user avatar
  • 6,810
8 votes
2 answers
258 views

Filling a buffer with a repeating pattern of N bytes

My program will clear the screen to a 24bit RGB color; in other words, it will fill a buffer with a repeating pattern of 3 bytes. So I wrote patternMemcpy to perform this operation efficiently*. It ...
Vortex 2728182818's user avatar
9 votes
4 answers
1k views

Reverse Engineering Data Files (Binary Visualization)

Binary Visualization based on Christopher Domas talk: youtube link and this Russian programmer where I discovered this talk: youtube link. Algorithm: Scan the pairs of bytes of a file with a sliding ...
Harith's user avatar
  • 6,810
2 votes
3 answers
682 views

Step-by-step re-arranging of a string with randomized characters

A sloppy code featuring algorithms to efficiently randomize the characters of a string "Hello C" and then print out in the console the re-arrangement process for every character. ...
Edenia's user avatar
  • 1,568
2 votes
2 answers
92 views

Generic stack implementation (revision)

The below post is a follow-up of Generic stack implementation. Below follows a header-only implementation of a generic stack (inspired by stb-libraries, following these guidelines: stb-howto.txt). ...
Harith's user avatar
  • 6,810
7 votes
2 answers
654 views

Consolidating GNU C's and C23's attributes

C23 has introduced attribute specifier sequences. Consequently, the header below attempts to conditionally define macros for these sequences (for my own use cases). In cases where a compiler does not ...
Harith's user avatar
  • 6,810
3 votes
1 answer
208 views

Generic stack implementation

The provided code (a header-only library inspired by stb libraries] defines functions for creating a stack, pushing elements onto the stack, popping elements from the stack, peeking at the top element ...
Harith's user avatar
  • 6,810
2 votes
2 answers
144 views

A small header-only input output library

The library (inspired by stb libraries) attempts to provide some commonly used functions (reading a file into memory, determining the size of a file) that are missing from the C standard library ...
Harith's user avatar
  • 6,810
4 votes
0 answers
102 views

Performs interpolation binary search over an array. If value found returns found and index else returns not found and insertion index

Performs interpolation binary search over an array sorted in ascending order. If value found returns found and index else returns not found, the insertion index is returned. Assumptions : data is ...
Pragmatic's user avatar
3 votes
1 answer
103 views

Read a line from a stream

Since the standard fgets() does not suffice for my use cases, as it doesn't automatically enlarge the target buffer if needed, and ...
Harith's user avatar
  • 6,810
7 votes
1 answer
397 views

Scientific Calculator

I finished this code a few minutes ago. It started off as an explanation of a basic calculator in C to a friend. Then we decided to take it further and I made this. Please tell me where I can make ...
ThePowerHex_YT's user avatar
2 votes
3 answers
88 views

Simple rwlock implementation in C

Can someone please review my rwlock implementation to see if there are any issues with correctness (hopefully not), give any feedback on how to write good C code etc, design patterns which could be ...
Tejas Anand's user avatar
4 votes
1 answer
305 views

Use of double pointers and memory allocation/deallocation

I've made an associative array data structure (more details in the code comments below). What I'm interested in getting a critique on is the usage of double pointers. Are they necessary here? Am I ...
denvaar's user avatar
  • 143
4 votes
1 answer
98 views

Writing a HTTP server in C

Code ...
Dang Quang Vinh's user avatar
2 votes
1 answer
109 views

Thread-safe strtok in C - version II

After improving Thread-safe strtok in C according to vnp's and Harith's nice comments, I ended up with this: Code strtok_arr.h: ...
coderodde's user avatar
  • 28.4k
3 votes
1 answer
114 views

A simple unit test library for C - version II

I have improved my code here and it looks like this: Code assertlib.h: ...
coderodde's user avatar
  • 28.4k
7 votes
3 answers
1k views

Thread-safe strtok in C

(This post has a version II.) Since strtok is not thread-safe, I decided to practice and write a thread-safe replacement. This code and the demo runner may be found ...
coderodde's user avatar
  • 28.4k
7 votes
1 answer
813 views

A simple unit test library for C

(This post is followed up by this one.) I have rolled this simple unit test library for C: com_github_coderodde_my_assert.h: ...
coderodde's user avatar
  • 28.4k
11 votes
2 answers
2k views

Brainfuck to C transpiler

I have been working on making a transpiler from Brainfuck to C in C(11), here is the code: ...
FFmpegEnthusiast's user avatar

1
2 3 4 5
80