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

Replacing spaces with tabs. Exercise 1-21 in “The C Programming Language”

I'm looking for feedback on the code I came up with for exercise 1-21 in "The C Programming Language" by K&R. Write a program entab that replaces strings of blanks with the minimum number of ...
4
votes
1answer
37 views

This program reads from serial port, Saves this to a text file, increments file name each time it is run

I want to condense it and get rid of any noob programming errors. The code works fine but I know it's not as elegant as it could be and I really want to improve. Your advice would be brilliant! I've ...
4
votes
2answers
37 views

Simple timestamping filter in C

I wrote a simple timestamping filter that prepends each line with the UNIX timestamp in nanosecond precision as returned by clock_gettime, as well as the difference to the previous timestamp. ...
7
votes
3answers
69 views

Very basic Integer queue in C

I've never written a queue before and literally just took the concepts and tried to code something up. I decided to write a basic integer queue and as an example/driver I use a queue of size 5 which ...
6
votes
5answers
109 views

Safe stack abstraction

Here is the stack abstraction that I've written. I've designed it to be safe and without undefined behavior. I use ugly_cast to discourage casting and to make ...
8
votes
3answers
645 views

Read string of any length in C

I've written code in C for reading a string of any length, and printing the string. It's one of my first C programs. I posted the program as part of a Stack Overflow question, and received a comment: ...
0
votes
0answers
23 views

How to avoid race conditions during UNIX process handling in C? [closed]

I'm trying to build a simple UNIX shell for educational purposes. My intention is to handle background processes correctly, displaying a message whenever it finishes it's job via the ...
4
votes
1answer
40 views

Evaluate mathematical expression in reverse Polish notation in C

This is a program exercise from The C Programming Language by Kernighan and Ritchie (Chap 5). It is based on my earlier question here on Stack Overflow. One user suggested to submit my code here, ...
6
votes
2answers
58 views

Function to prompt user for input

The following program calls a function, which prompts the user for input. The function then returns one of three enum constants; INPUT_SUCCESSFUL, ...
5
votes
1answer
77 views

Convert a hex string to base64

I wrote a program that converts a hex-encoded string to base64. It's my solution to the first of the Cryptopals challenges. My main concerns are: Portability. I don't want to rely on implementation-...
2
votes
2answers
90 views

Get the Nth Term in the Fibonacci Sequence

I'm trying out a new editor (CLion) and I decided to try an old well known programming problem. I also decided to try out C99 rather than C89. This program calculates the nth term of the Fibonacci ...
7
votes
2answers
104 views

K&R2 solution to exercise 1-18 (discarding trailing whitespace and empty lines)

Recently I have started learning C++ and I decided to learn C along the way to improve my understanding of how computers work. For C I am using "The C Programming Language Book" by Brian Kernighan and ...
8
votes
0answers
134 views

spawn() for gawk

Awk has a builtin function named system which makes it possible to run shell commands from awk scripts, but it is completely unsafe to build commands using user-...
2
votes
3answers
62 views

Getting the Sum of Discounted Price

Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third ...
10
votes
2answers
1k views

Converting time in number of minutes past midnight to HH:MM format

This program is a clock that only displays minutes passed since midnight. This program is supposed to be really simple but because of some "aesthetic bug", I wrote quite a lot if-else statements that ...
4
votes
2answers
51 views

Loading and drawing an image

I have some functions and structs for loading and drawing an image with OpenGL. The two main places I would like feedback are: I've tried to separate the OpenGL-specific code into the implementation ...
3
votes
2answers
69 views

Computational verification of Collatz conjecture using gmplib

Prerequisites The unsigned long type has a size of 64 bits. The following macros are defined. ...
1
vote
2answers
95 views

Validation of credit card number

I have written the code for credit card number validation and it works correctly but I feel that the code could be reduce to fewer lines. ...
6
votes
2answers
81 views

Safe and simple strtol()

Until today, in the few cases where I needed something like this, it had been in simple programs which only I used, and where I didn't care about security, so I used the simple ...
5
votes
1answer
41 views

POSIX+ implementations of echo(1)

Ever wanted a strictly conformant echo(1) implementation on your system? Wait no more: I've built it! What is it? My first large-ish body of C code in a while. ...
3
votes
1answer
257 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
5
votes
2answers
119 views

Quick Sort in C

I'd like to know if there's any issues with this implementation of qsort_r (which is not available in all implementations, so I'm trying to provide one that allows ...
-1
votes
2answers
56 views

Format and return a string from a function in C [closed]

Write a function that will format a string and return it A fixed length of string is malloc-ed. snprintf is used to format the string and then the buffer is ...
6
votes
3answers
139 views

Taking out number of subarrays from an array which contains all the distinct elements of that array

I am a beginner in C. My teacher gave me a homework problem in which a user would provide an array with its size. What we have to do is:- 1) Take out all the distinct elements from that array. ...
9
votes
6answers
525 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 ...
3
votes
1answer
32 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
485 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
44 views

Return Function Pointer based upon String [closed]

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 ...
11
votes
1answer
233 views

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 ...
5
votes
3answers
74 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. ...
2
votes
1answer
76 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 were not present and I am aiming for backwards compatibility to GLSL 1.2. ...
1
vote
4answers
132 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 ...
-1
votes
1answer
31 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}\...
2
votes
2answers
49 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 ...
5
votes
3answers
113 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 ...
5
votes
4answers
191 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 ...
7
votes
3answers
334 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 ...
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 ...
2
votes
2answers
77 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 ...
4
votes
1answer
67 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
70 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
51 views

Linked HashMap in C

I was looking for comments (on performance and general implementation) on the following Linked HashMap implementation in C: ...
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 ...
4
votes
2answers
73 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? ...
4
votes
1answer
50 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: ...
3
votes
1answer
102 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. ...
1
vote
1answer
49 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
591 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 ...
1
vote
1answer
54 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 ...