Skip to main content

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
1 vote
0 answers
50 views

Makefile for C project

This is a makefile for a project I'm currently working on. I was wondering, if there was a way to write it more concisely, especially concerning tests and libraries. I would like to maintain the ...
vdrummer4's user avatar
2 votes
2 answers
97 views

Generic Dynamic Array Implementation

I wrote a dynamic array implementation in ISO C11 using void pointers. Everything I've implemented works--at least in all my tests--on a 64-bit machine. It has some vague type-checking and resizes ...
Zenais's user avatar
  • 23
3 votes
3 answers
273 views

Eliminating "implicit conversion changes signedness" warning using _Generic

I'm using enum in C to have constants for values that can be bitwise-or'd together, e.g.: ...
Paul J. Lucas's user avatar
6 votes
3 answers
718 views

Finding the Zeckendorf Representation of a Positive Integer (BIO 2023 Q1)

I was practicing question 1a of the British Informatics Olympiad 2023 past paper. In the Fibonacci sequence each number is generated by adding the previous two numbers in the sequence. We will start ...
sbottingota's user avatar
5 votes
1 answer
357 views

String Decryption in C (BIO 2022 Q1)

I was practicing question 1a of the 2022 British Informatics Olympiad past paper. Each letter in the alphabet can be given a value based on its position in the alphabet, A being 1 through to Z being ...
sbottingota's user avatar
4 votes
3 answers
635 views

Error handling for singly linked list in C

I have seen many list implementations in C in this site; I know its been asked many times. I need some advice regarding: Quality of my code, especially my list library. How to handle errors in main (...
Sai_krishna's user avatar
3 votes
1 answer
250 views

Writing a generic makefile for C projects

Directory Structure: . ├── check.sh ├── GNUmakefile ├── Makefile └── src Makefile has these contents: ...
Harith's user avatar
  • 9,462
6 votes
3 answers
971 views

Implement Huffman code in C17

I just finished my first coding project. Prior to this I have only made small programs so I'd like some feedback on what to improve on and how I did in general. The whole code is 658 lines long. The ...
TheGlibber's user avatar
3 votes
0 answers
133 views

Copy a file portably across UNIX and UNIX-like systems with POSIX API (modeled after Boost's copy_file())

Overview: Following the POSIX API, there are 2 versions of the function: unix_copy_file(), and unix_fcopy_file(). One works with ...
Harith's user avatar
  • 9,462
7 votes
2 answers
712 views

Wrappers around write() and read() and a function to copy file permissions

Below follows wrappers around read() and write() that retry when interrupted by a signal (the case where the return value is -1 ...
Harith's user avatar
  • 9,462
1 vote
0 answers
39 views

Determining the value of PATH_MAX on UNIX-like systems

Below is the approach taken in the function allocpath() in the book "Advanced Programming in Unix Environment", which I have modified to simply determine ...
Harith's user avatar
  • 9,462
3 votes
2 answers
107 views

Get Terminal Size, Enable and Disable Terminal Raw Mode without NCURSES

I searched around and found these 7 ways to determine the width and height of the terminal: ...
Harith's user avatar
  • 9,462
6 votes
2 answers
398 views

Family, felon & fuzz River Crossing challenge

I first saw this challenge when a user posted it to SO about two years ago without a shred of code. The post's plea, "How do I get started?" meant the user's post didn't survive long on that ...
Fe2O3's user avatar
  • 1,468
3 votes
1 answer
72 views

A quine that clones itself and runs some more quines

This is a follow-up to: A Quine program in C, where today I received the following suggestion: Now, write the code so that the binary "clones" itself (makes a replica with an altered ...
Harith's user avatar
  • 9,462
9 votes
3 answers
514 views

Form all possible well-formed strings of 'n' nested pairs of 2 types of ASCII brackets

Taking an interest in the challenge presented in this recent Code Review question, I've concocted a C solution (valid as C++, if I'm not mistaken) seeking to reduce both the lines of code and the ...
Fe2O3's user avatar
  • 1,468
5 votes
1 answer
547 views

A Quine program in C

From Wikipedia: A quine is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and ...
Harith's user avatar
  • 9,462
11 votes
2 answers
3k views

Scheme interpreter in C

I created a basic Scheme interpreter in C ...
Dean Menezes's user avatar
3 votes
1 answer
213 views

An example of hello world LKM

In 2021 I spent a lot of time studying low-level in Linux, as I'm a low-level addict in general, I only now decided to change my area to introduce myself to the market, and I wrote a repository on ...
Bruno Ciccarino's user avatar
1 vote
0 answers
55 views

General Purpose Utility Constants, Macros, and Functions

Overview: I have bundled together different macros and functions I often require in different projects into a single compilation unit (utils.h and ...
Harith's user avatar
  • 9,462
4 votes
3 answers
162 views

Type Traits for C with _Generic

Motivation: Type traits are useful in defining robust function-like macros. Code below has: IS_COMPATIBLE(EXPR, T) IS_NULLPTR(T)...
Harith's user avatar
  • 9,462
2 votes
1 answer
67 views

Schedule using binary tree in C

I'm trying to organize the code according to the run menu of a schedule using the binary tree data structure because if I search for a user, the menu always appears first, and the listed user is left ...
user avatar
11 votes
2 answers
2k views

NES Emulator in C

I've been coding an NES emulator for a couple of weeks, and would like to have some feedback on what I have written so far. I don't have any experience in C other than a few very small programs, so I ...
Leon9343's user avatar
  • 155
6 votes
3 answers
164 views

An Attempt at Creating a Type-Safe, Generic Swap Macro With Compile Time Error Checking

Properties: It evaluates each of A and B only once (sizeof is special, as it doesn't ...
Harith's user avatar
  • 9,462
1 vote
1 answer
55 views

Generic Min() for Integer Types (including Boolean)

This is a follow-up to Generic Max() for Integer Types (including Boolean) and An Attempt at Creating Generic Min()/Max() for Fundamental Types. What's new: MIN(), ...
Harith's user avatar
  • 9,462
8 votes
2 answers
171 views

Generic Max() for Integer Types (including Boolean)

This is a follow-up to An Attempt at Creating Generic min()/max() for Fundamental Types. What's Changed: MIN() has been forsaken for the time being. Mixed types ...
Harith's user avatar
  • 9,462
7 votes
8 answers
2k views

Convert string name to uppercase

As part of an assignment, I wrote this helper function to convert a specified name to a required format. This function simply converts a string in the format "this-string" to "This ...
Linny's user avatar
  • 10.3k
0 votes
0 answers
18 views

Stub-Based Unit Testing 5 Read Routines (revision)

This is a follow up to: Stub-Based Unit Testing 5 Read Routines. What's Changed: OP's goal of reducing the source files' count has been achieved. There is now a single test source instead of 18 ...
Harith's user avatar
  • 9,462
3 votes
2 answers
324 views

Makefile to build and test a C program

Requirements: make shalt build read_file program in the root directory of the project from all the files in the ...
Harith's user avatar
  • 9,462
13 votes
4 answers
2k views

C - CHIP8 Interpreter

I want to get into emulator development, so I tried to write a CHIP-8 interpreter in C. I implemented it using this guide, and tested it using the test programs provided here (the test cases pass). ...
wed1may's user avatar
  • 195
4 votes
3 answers
141 views

Stub-Based Unit Testing 5 Read Routines

Requirements: Following are the functions I need to test, along with the cases where they can fail: ...
Harith's user avatar
  • 9,462
0 votes
1 answer
72 views

DIY memmove(). (Asking for a friend, kinda...) [closed]

A few days ago, this answer was posted to a 2y.o. Stack Overflow question (public domain). (The question about a presumed requirement of an intermediate buffer to implement ...
Fe2O3's user avatar
  • 1,468
8 votes
2 answers
1k views

Aho-Corasick algorithm implemented in C

I've been reimplementing the fgrep utility in C for fun & study, and so had to implement the Aho-Corasick algorithm. I'm ...
Knogger's user avatar
  • 285
8 votes
7 answers
2k views

Merge function in C

The task was as follows: Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array output parameter (still in ...
Peter Kirsch's user avatar
9 votes
4 answers
931 views

An Attempt at Creating Generic Min()/Max() for Fundamental Types

The simplest way one can achieve this is to have separate functions for each type with a suffix to differentiate between them like the following: ...
Harith's user avatar
  • 9,462
6 votes
1 answer
561 views

Play Snake Game on Mac OS Terminal using C with ncurses Library

Made a simple snake game in C. I am just having some fun with program and using this as a spring board of knowledge in hopes to get some confidence to maybe try different kinds of projects. I am ...
Alf's user avatar
  • 63
8 votes
4 answers
600 views

DIY Sudoku Solver (Windows Console App)

This will be somewhat larger than many Code Review posts. In a recent CR thread, an invitation was extended for me to post my DIY Sudoku solver. For the fun of it, here we are... I'm buzzing with ...
Fe2O3's user avatar
  • 1,468
3 votes
2 answers
69 views

Reading a file into lines (benchmarking getline() vs mmap_memchr() vs mmap_getline() vs fread())

This is a follow-up to: Reading a file into lines with mmap vs getline. The actual names of the functions are not ...
Harith's user avatar
  • 9,462
4 votes
3 answers
713 views

Implementing strscpy(): Is using errno as a negative return value a bad practice?

strscpy() is similar to the standard strncpy() except that it always writes a valid null-terminated string (unless ...
ismbks's user avatar
  • 43
3 votes
3 answers
340 views

Append Buffer for Buffering Small write(2) Calls

The purpose of this is written in the comment below (it is meant to be used in a text editor): abuf.h: ...
Harith's user avatar
  • 9,462
1 vote
0 answers
45 views

Emulating C23's QChar * behavior for basename and strchrnul

To quote @Lundin from What is C23 and why should I care?: Bug fixes for a lot of library functions (search functions in particular): we can now pass a const-qualified pointer parameter to a library ...
Harith's user avatar
  • 9,462
7 votes
1 answer
232 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
  • 9,462
6 votes
1 answer
636 views

Karatsuba algorithm in C

This is the Karatsuba algorithm I wrote: ...
Talete's user avatar
  • 63
4 votes
4 answers
328 views

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

C23 standarized strdup()/strndup(), but left out stpcpy(). ...
Harith's user avatar
  • 9,462
3 votes
1 answer
50 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
  • 31
2 votes
1 answer
43 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
  • 9,462
4 votes
2 answers
571 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
  • 9,462
4 votes
2 answers
81 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
  • 9,462
5 votes
2 answers
555 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
180 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
  • 9,462
5 votes
1 answer
182 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
  • 9,462

1
2 3 4 5
81