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
2
votes
1answer
65 views

Stored Enchantments

I am making a game, and I decided to totally copy Minecraft's enchantment system. Other enchantments you might see are ones I am adding to my game. I have a few candidates for storing enchantments ...
9
votes
6answers
455 views

Parse a simple key=value config file in C

Write a program that should read a file formatted strictly like this below: $ cat sample bingo=2 bingo2=939 bingo3=text The left and right side of ...
7
votes
0answers
81 views

Error handling function (Win32/C)

I have written the following function to be a general "error logging" solution for my Windows API projects. Basically, given a Windows system error code (a la ...
-4
votes
0answers
31 views

Not able to find the test cases where my code will not give the right answer [on hold]

Recently I was solving problem on SPOJ: Problem Link: SUM GCD I need to calculate the value of the following equation: $$\sum_{i=1}^{n-1} \sum_{j=i+1}^n gcd(a[i],a[j])\; \text{where } 1\leq n\leq100$$ ...
5
votes
4answers
180 views

C-based hash matching cracker

How can I improve this hash matching program using either abstraction or search/sort algorithms? We briefly were introduced to searching methods such as bubble sort and merge sort, but I'm not sure ...
11
votes
1answer
178 views
+100

Small C project: recording mouse/keyboard bot software

This is, kind of, my first programming project. It's a small project to complete first year's university programming course. The program allows the user to record his mouse/keyboard/cursor ...
2
votes
1answer
23 views

Implementing sbrk for a custom allocator in C

I took on a for-fun task of creating a memory allocator which uses process memory rather than making a bunch of system calls to the operating system. The task was: implement my own ...
4
votes
2answers
477 views

Partitioning and sorting even and odd numbers within an array

I solved a problem where it was told to sort an array of numbers such that the even numbers are kept first and the odd numbers should follow. Moreover, the even numbers must be sorted in ascending ...
1
vote
1answer
38 views

Return Function Pointer based upon String [on hold]

I have the following function that takes a string and returns a function pointer; currently its a long if-else block; thats continuing to grow this will make it difficult to maintain. The logic of ...
5
votes
3answers
67 views

Unix tail command using stack

I'm working on a version of the Unix tail command that works by iterating over a file, pushing line by line into a stack. Finally, the number of desired lines will be popped and printed to the screen. ...
1
vote
4answers
104 views

“String” contains function in C

This function checks whether or not a substring needle exists in another string haystack and returns the position if it does or ...
6
votes
5answers
1k views

Macro for allocation in C

As we all know, the syntax of allocating memory is a bit clunky in C. The recommended way is: int *p; int n=10; p = malloc(n*sizeof *p); You can use ...
1
vote
0answers
53 views

Convert an integer to 4 bytes without bitshift operators

This code is applicable to either GLSL or C due to virtually identical syntax. Before GLSL 1.3, bitshift operators where not present and I am aiming for backwards compatibility to GLSL 1.2. ...
-1
votes
1answer
29 views

Client / server message parser

how could I optimize my code, or make it cleaner? See things to avoid. I receive data in my protocol and then analyze it in a message structure. My protocol: {2 octet: Message ID}{? octets: CONTENT}\...
5
votes
3answers
111 views

File compression project

I wrote a general purpose file compression program out of curiosity, but I am concerned about readability. I don't know if the variable names are useful, and whether most of the comments are unneeded ...
10
votes
3answers
2k views

Yet another hash table in C

Will be very grateful for thoughtful code review. I needed hash table (again) and I wrote one. Code here is here or inline: ...
2
votes
2answers
4k views

Minimum number of swaps required to sort the array in ascending order

Here is my problem statement. An excerpt: You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are ...
2
votes
2answers
41 views

Hash table implementation in C for a simple table record

I have a requirement to implement this simple table from one of the DVB S2X standards. The table is relatively small that contains no more than 28 rows. I need to use the key for the table as the ...
7
votes
1answer
58 views

Custom malloc implementation

I have a malloc() implementation that relies purely on mmap. I'm fairly sure that it doesn't overwrite any of the internal ...
2
votes
2answers
67 views

Production-ready comparison between byte-array and ByteBuffer

I want to compare the performance difference between using byte[] and ByteBuffer in Java, which approach I should use in my ...
7
votes
3answers
324 views

IP4 strings validator

Trying to write fast non dependency IP4 string validator for embedded system, It should counts leading zeros, values over 255 and other corrupted strings. Not sure if I missed something. Сan you guys ...
13
votes
3answers
175 views

Detecting unicorn and dinosaur compilers

Rumor is that the next version of C will disallow sign magnitude and ones' complement signed integer encoding. True or not, it seems efficient to not have to code and test for those rare encodings. ...
1
vote
1answer
75 views

Fixed-Size Floating-Point Types

I have created my own personal data structure libraries for C and I've re-engineered it about two times now and I've added alot of preprocessor checks and defines concerning OS type and compiler type. ...
10
votes
2answers
1k views

Piece of chess engine, which accomplishes move generation

This is a piece of a chess - engine which accomplishes the generation of all (yet unvalidated) moves given a legal board. However, I have the strong doubt this is well written. The following action ...
11
votes
2answers
3k views

Hashtable Implementation - C

HashTable implementation The Specifics: It uses a simple hash function, it recieves a string and adds the ASCII values of each caracter, this can cause problems if the string is big, but for this ...
4
votes
1answer
58 views

Insertion Sort in C

I have started studying algorithms using the classic CLRS book and decided to hone my C skills at the same time. I wrote this function implementing insertion sort in C. I have tested it and verified ...
3
votes
0answers
61 views

Emulating a C++ enum class C++ in C

I am trying to emulate a C++ enum class in C and the code works as I expect. I used this code as indicated on Stack Overflow. ...
0
votes
1answer
44 views

Linked HashMap in C

I was looking for comments (on performance and general implementation) on the following Linked HashMap implementation in C: ...
-1
votes
2answers
90 views

Cracking the code interview: Problem 1 .6 basic string compression

I created a GitHub repository containing my code solutions for some of the exercises proposed in the book. I updated my solution for the problem 1.6 here. Description A function that compress the ...
4
votes
0answers
68 views

Generic Macro Generated Interval Heap in C

When I saw this amazing data structure I couldn't stop myself from trying it! I first sought resources such as this and an article about it here. It basically works as a Max-Heap and a Min-Heap at the ...
4
votes
4answers
1k views

Unix chat server making communication between terminals possible

This Server handles clients communicating with each other. Because I was not willing to actually give every client a name, they are named by their socket's file descriptor. This is my first ...
3
votes
0answers
358 views

Attempt at multithreaded memory allocator

I'm trying to learn C and memory management, therefore as an exercise I've written a memory allocator. I've taken the best fit algorithm and added multithreading with locks. I'm a beginner with ...
4
votes
1answer
48 views

Evaluating π using Monte Carlo methods - Serial vs OMP

I wrote this simple code for evaluating the π using Monte Carlo method. This is the serial version: ...
4
votes
2answers
68 views

Array of all permutations in C

Is this a good compromise between readability and verbosity? Are there any bugs, additional error checking, or extraneous code as well? ...
1
vote
1answer
48 views

Simple protocol client library

Overview I got rubber-stamped in code review today even though I called out specific concerns about my own code and asked for advice, so I'm turning to this community for advice. Thank you for taking ...
7
votes
3answers
571 views

Cracking the Coding Interview — 1.5 One Away

I would like to know any suggestions about to improve my code solution and your rating of my approach to the problem, I based my code on the fact that if the strings have different length removing one ...
6
votes
4answers
1k views

Parse source code of the RAPID robot-automation language

I am new to C, and I have very little formal education on programming (Although I am currently in college). I work as a Robot Automation technician, and me and my team are required to document a lot ...
4
votes
1answer
93 views

Hashtable implementation in C for generic values

I am trying to implement a hashtable that supports character keys and can return any type of value contained in a struct as long as it contains as long as it contains ...
1
vote
1answer
49 views

Serial port library for unix systems in C using callback model

I wrote a blocking serial port library, Blocking serial port C library, but prefer non-blocking so wrote this library where user can specify callbacks. So it is event driven. The code assumes C99 or ...
5
votes
3answers
184 views

Encapsulating snprintf to avoid repetition of sizeof

Correct usage of snprintf involves very long and repetitive lines: ...
3
votes
3answers
1k views

Extracting the longest line from standard input

I'm reading the K&R book and I'm interested in a program which finds the longest line on the input. I've written the same code as in the book, but I wanted to do it myself. I tried to write my own ...
4
votes
1answer
34 views

timer struct for fixed timestep iterations in C (gcc)

I was toying with a way force fixed timestep loop iterations in C and came up with a fairly simple timer struct (quite different from Pausable Timer Implementation for SDL in C and decided to put it ...
-5
votes
1answer
114 views

Console snake game implementation C [closed]

I just ended the console snake game and want to review it. I hope you'll review my codestyle (e.g. I don't know what to do with #ifdef inserts in ...
16
votes
6answers
2k views

Avoiding repetition when using the “snprintf idiom” to write text

I'm writing a function to stringify a nested struct. I'm using snprintf to pre-calculate how much space I'll need to allocate. The problem is, it's extremely ...
4
votes
5answers
1k views

Thread synchronization with mutex

This program prints odd numbers by thread1 and even numbers by thread2 sequentially. Can this code be optimized or made more ...
3
votes
3answers
243 views

Frequency analysis based anagram checker

I uploaded code solutions for some problems of the book Cracking the Coding Interview, 6th Edition to GitHub, I would like to know your rating and potential improvement of the code I wrote. Here is ...
4
votes
3answers
300 views

Simple mutex and conditional variable signal in C

I'm new to multithreading in C so I made a toy program that uses a mutex and a conditional variable to communicate between two threads. do_work performs a task ...
4
votes
4answers
6k views

Socket client in C using threads

I'm working on socket programming in C. I have no problem with usage the threads. This all works fine but I'm new in this area. I wrote this code in client.c but is there any misused code or something ...
8
votes
2answers
451 views

Simple Unit Test in C

Simple UnitTest macros utility in C. I decided to make this to be able to more easily organize my tests and I emphasized on readability. Please tell me what you think and if this utility would perform ...
9
votes
2answers
119 views

Generic data structures in C

I'm new to C, and I thought a great way to learn would be to implement some generic data structures and algorithms. So far I've implemented a linked list and a dynamic array. Both are generic, the ...