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
0 votes
0 answers
12 views

Extensible typing system for a strongly-typed DSL

I recently finished cleaning up my code in a compiler/interpreter for a strongly-typed domain-specific language, which makes use of three sorts of type: Compile-time enumerated types: Have one of a ...
  • 109
3 votes
2 answers
69 views

C macro based dynamic array library

This library is a fork of eteran/c-vector. It is macro based, so that it is generic, without void pointers and additional functions. This of course means that it has the downsides of macros in C. The ...
1 vote
2 answers
95 views

Logistic regression using gradient descent

My code seems to work fine, but I'm interested in how I can make it faster. I really just want to reduce any wasteful computation (for instance ...
0 votes
1 answer
51 views

(Dog) Agility - Penalties Calculator

I have written the following code and wanted to ask for alternative ways of completing the given task, since I think that my code - even if "properly working" - is quite "brute" ...
3 votes
2 answers
81 views

Follow up: A chat server using the select() API

This is a follow up to my last question: Multiperson chat server using the select() API Changes: After much fine critique, I made the following changes to the code: Removed redundant casts and ...
  • 377
8 votes
1 answer
298 views

Solution to Codejam 2019 1A (Pylons) in C

The following is my solution to the Pylons problem from Codejam 2019: https://codingcompetitions.withgoogle.com/codejam/round/0000000000051635/0000000000104e03. Our Battlestarcraft Algorithmica ship ...
  • 265
7 votes
2 answers
708 views

Event functionality in C

I am beginner to intermediate and wanted to write a little event code in c. It's one header file with 108 line pure code and it does what I was thinking it should be like for me. EventListener.h ...
3 votes
0 answers
39 views

Music Library w/GTK GUI in C

Hello everyone I've started learning a little bit of C, and I've made pretty simple project, that have a lot of potential in building. I would like to add some features in the future, such as playing ...
1 vote
0 answers
30 views

Stack using linked list in c [closed]

I want to see if there can be any amelioration of this stack implementation. I especially want to see if there is anyway to make this code shorter and safe, I can't see how myself, but it's up to you. ...
11 votes
2 answers
1k views

Multiperson chat server using the select() API

I followed the Beej's Guide to Network Programming. The program acts like a multi-user chat server. One connects and sends a message to the server, which is forwarded to all that are connected. There'...
  • 377
3 votes
1 answer
40 views

Wipe a directory tree

The idea here is to destroy the data in a directory tree by finding any files in it and overwriting their data with garbage before deleting them. The function returns minus the number of errors ...
0 votes
3 answers
125 views

Simple stack of integers

I've a simple push/pop implementation in my program: ...
6 votes
2 answers
218 views

Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing

My attempt at Exercise 1-21 in the The C Programming Language book written by Brian Kernighan and Dennis Ritchie. Exercise ...
6 votes
2 answers
950 views

Follow-up: C program for storing the time of the sunset and sunrise

This is a follow-up to the question: C program for storing the time of the sunset and sunrise After receiving a lot of great help and critique I've updated my code. Questions Are there major issues ...
  • 91
5 votes
1 answer
291 views

Write a Program Detab That Replaces Tabs in the Input with the Proper Number of Blanks to Space to the Next Tab Stop

Continuing my K&R journey! Please see below for my solution to: Exercise 1-20: Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop....
3 votes
4 answers
917 views

C program for storing the time of the sunset and sunrise

I just wrote my first C program for storing the time of the sunset and sunrise for a location given in latitude and longitude in a SQLite database. I usually write C# only. Since this runs on a ...
  • 91
4 votes
3 answers
125 views

Simple photomosaic generator

I wrote an implementation of Robert Heaton's Programming Exercise for Advanced Beginners #4 (photomosaics) in C99 using the MagickCore library. The full code is as follows: ...
  • 43
3 votes
3 answers
87 views

Parse program arguments of the form key=value

I have two proposals for parsing arguments: Method A ...
2 votes
1 answer
85 views

C generic typesafe dynamic array (vector) try 2

The previous generic dynamic array I asked a review for here was written using only the preprocessor and so while the array itself was type-safe, all the manipulating macros where not. In this try I ...
5 votes
6 answers
540 views

Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time

Continuing to slowly progress through The C Programming Language by Brian Kernighan and Dennis Ritchie. The code I came up with below is for ...
6 votes
5 answers
1k views

Rock, Paper, Scissors game with CPU

I have written the below shared code and wanted to ask for some optimization tips or even alternative (more elegant?) ways of solving the task at hand (maybe without ...
6 votes
2 answers
145 views

Finding the sum of 2^x and all powers of 8 less than 2^x

Is there a more efficient way, without a loop for example, to find a the end value for n for a specific x? ...
5 votes
1 answer
63 views

32x32x32 units octree, that supports 15-bit data units

The idea is to have an octree that stores a 32x32x32 region of 15 bit values more efficiently than a 32x32x32 array, because if all 8 leaf nodes of a given branch are the same, the branch does not ...
4 votes
2 answers
96 views

Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines [closed]

Continuing to slowly progress through The C Programming Language by Brian Kernighan and Dennis Ritchie. The code I came up with below is for ...
3 votes
2 answers
72 views

Follow-up about computing lp norm with MPI

As pointed out in the answer by Victor Eijkhout to this question, the fact I am broadcasting the same vector to each process is against the MPI principles and, most importantly, will be a bottleneck ...
3 votes
1 answer
229 views

Compute lp-norm of a vector in parallel with MPI

I'm trying to solve the following exercise: Compute in parallel with MPI the lp norm of a vector of a predefined size N ...
3 votes
2 answers
107 views

Something like std::bitset in C

I am looking for opinion on this bitset implementation. It only has 4 functions and they are set - set a bit/clear - clear a bit/check - test a bit /zero - zero out all bits. There is another one as ...
6 votes
1 answer
292 views

Write a program to print all input lines that are longer than 80 characters

Like many posts before, I'm going through The C Programming Language, 2nd Edition, by Kernighan and Ritchie. I am very new to the ...
4 votes
2 answers
161 views

128-bit integer type in GCC/Clang

When working on my projects, I often need a 128-bit integer type. For this purpose, I use the __int128 compiler extension. Here is the implementation I am currently ...
  • 289
3 votes
1 answer
40 views

Matrix-vector multiplication with block-column distribution and MPI_Reduce_scatter

I'm studying MPI and I'm trying to solve the exercise (number 3.5) taken from Pacheco's book Introduction to parallel programming. Essentially the exercise is: Write an MPI program that computes the ...
-1 votes
1 answer
73 views

Safe and portable way to write a file in C [closed]

I've read that fopen() is deprecated, and fopen_s() is recommended in its place. But there are many posts outlining the problems with fopen_s() in Linux. Several tutorials just avoid mentioning that ...
2 votes
2 answers
49 views

semaphore barriers exercise os three easy pieces

I am reading the book OS three easy pieces, In the semaphore chapter I am going through barrier.c question. Below is the code that needs to be completed for the desired output: ...
4 votes
1 answer
69 views

C void* Generic Hash Table

Some time ago I tried to make a generic linked list in pure C (no external libraries, only the C standard library) using void*s here. Building on top of that and ...
2 votes
2 answers
64 views

C Arena (pool allocator)

I wrote a pool allocator (Arena) for a compiler I'm writing. My use case for this allocator is for storing the parse tree because it allows me to free all the memory at once, so I don't need to keep a ...
9 votes
3 answers
1k views

A Simple C Program to Find Factors of any Integer

I have written a simple CLI app that finds factors in 3 different forms. I would like the code to be reviewed. Here's the code: ...
5 votes
5 answers
519 views

Find the 10 largest files in a directory tree

I've recently finished a small project intended to better understand C. The program will output the ten largest files found (recursively in the event of subdirectories) in a specified directory. I've ...
8 votes
1 answer
485 views

C void* Generic Linked List

I once tried to make a generic linked list in pure C (no external libraries, only the C standard library) here using C macros. With the same restrictions as in the previous attempt, this time I'm ...
2 votes
3 answers
66 views

Resizable string object

...
4 votes
2 answers
648 views

Implementation of Hash Function in C language

I'm new to programming. and I'm learning a C language. Currently working on Harvard's CS50 problem set (this one: https://cs50.harvard.edu/x/2022/psets/5/speller/). In that problem, they want us to ...
  • 143
7 votes
1 answer
125 views

Chess position representation with FEN parser

I'm developing a UCI chess engine and as part of my engine I need a position representation that stores the position of every piece on the board and all the information about the game, like castling ...
  • 108
11 votes
5 answers
2k views

Implementation of a two-dimensional array in C

I implemented a two-dimensional array in C and would like to know if the implementation is sound in terms of Memory management Potential bugs and other side effects that I missed Code style (...
  • 1,125
4 votes
1 answer
49 views

Print vertical histogram of inputted words…

From The C Programming Language, by Kernighan and Ritchie: Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars ...
  • 143
3 votes
2 answers
116 views

A stripped-down version of UNIX sort utility

Usage: ./usort <options> <filename> Name: ./usort - sort lines of text. Description: Write sorted concatenation of FILE to standard output. ...
  • 377
6 votes
2 answers
255 views

C safe getline()

For a recent project of mine I had to do read input from the console in pure C with no external libraries (in other words, code I've written by myself). I don't like the standard formatted input such ...
2 votes
2 answers
88 views

Finding a function from a name within a string

The code is designed to see if the first alphabetic characters in the *str argument refers to a known function, as part of a calculator app, and sets ...
5 votes
2 answers
115 views

C Macro Generic Linked List

For a recent project of mine I had to create various data structures in pure C with no external libraries (in other words, code I've written by myself), which lacks the templating and OOP capabilities ...
2 votes
2 answers
77 views

Return a random integer with a probability progression

A function random_int_with_probability that returns a random number within a range, where the chance to get a bigger value decreases linearly towards the maxima. In ...
  • 1,538
5 votes
3 answers
672 views

Number System Conversions with strtol

Description The code uses strtol() to convert text that represents a value in a given base into an integer. It takes three arguments from the command line, ...
  • 377
5 votes
1 answer
73 views

Möbius function using iterators for prime factors

While working on Project Euler, I have come across a few questions involving usage of the little omega function, big omega function, and Möbius function. My prior code wrote factors into an array and ...
2 votes
1 answer
150 views

C/C++ hash looking in flat database compared to PHP

I have a hash table which I would like to query as fast as possible. The hash table is stored this way : When I query a hash, I extract first 4 characters then use it to find the file. For instance a ...
  • 41

1
2 3 4 5
77