Questions tagged [c]
C is a general-purpose computer programming language used for operating systems, games and other high performance work.
1,303
questions
0
votes
1
answer
113
views
Is interleaving local variable declarations with assertions and function calls a bad practice?
In my experience, it is customary to place local variable declarations at the beginning of their scope. Several questions in this forum ask whether this needs to be so, and their answers tend to agree ...
7
votes
2
answers
2k
views
How can I make code that is both DRY and fast where intermediate values in a calculation may or may not be needed?
How can I make DRY (lacks repetitive patterns) code that also avoids inefficiencies from using intermediate values in a calculation that might not need to be used?
Here is an example:
In this code, I ...
19
votes
5
answers
9k
views
How can Rust be "safer" and "faster" than C++ at the same time?
I have been told that Rust is both safer and faster than C++. If that is true, how can that be even possible? I mean, a safer language means that more code is written inside the compiler, right? More ...
0
votes
1
answer
132
views
Is it anti-pattern to obtaining public static data from a function?
In C programming, I have a set of information, and I have to ways of providing it to user:
construct a data structure and provide it as an object.
write a function to read them out and return them.
...
1
vote
1
answer
72
views
How to return a result from an active object state machine
I frequently use the concept of Active Objects (https://www.state-machine.com/active-object) combined with state machines when designing code. The key idea behind these is that only "events"...
0
votes
2
answers
220
views
How to combine epoll + multithreading in an HTTP server
I'm building an HTTP server in C using epoll and pthread. The idea is to have an event loop in the main thread monitoring file descriptor and a thread pool to handle computationally-expensive ...
4
votes
1
answer
273
views
Dealing with global variables required by badly-written library
I am working with a library that is somewhat poorly written. In order to function, it requires several global variables to be declared and sometimes even maintained by my own code. I really don't ...
-1
votes
1
answer
92
views
Why use the same identifier for a macro AND a function (GNOME source code)?
I just happened to stumble across the following question on stack overflow:
In gatomic.c of glib there are several function declarations that look like this:
gboolean
(...
1
vote
4
answers
267
views
Passing arrays as global variables instead of pointers in C
I'm required to write the Low-Level Requirements of a Software Component which shall perform signal processing over arrays of 200k elements of integers/floats which lives in the main memory of the ...
1
vote
3
answers
187
views
TDD - What to do when adding a new function on a dependency causes many previous tests to fail?
I was programming today and encountered something that just feels like I'm doing something wrong (maybe?). I've encountered this situation before, but I wanted to reach out and ask if there's a better ...
1
vote
0
answers
82
views
Mimic public/private data members in C with hidden static arrays
Context
I'm designing the software architecture of a safety critical software written in C, under DO-178C (DAL A) and with a Code Standard based on MISRA-C 2012, but no fully compliant.
It is the ...
4
votes
1
answer
315
views
What is the problem with whitespace in C that Ruby allegedly repeated?
I'm reading the book The Secret Life of Programs by Jonathan E. Steinhart. In it, he mentions in passing:
many consider the handling of whitespace in Ruby to be a replay of of a mistake in the ...
0
votes
2
answers
200
views
Why are arguments of fopen restrict-qualified?
It has caught my attention that both arguments of fopen are both restrict-qualified. It's been that way since C99.
What does this achieve?
If I have:
const char *path = "foobar";
const char *...
0
votes
0
answers
26
views
Is there any reason to namespace structure members in C?
In C, such as in many POSIX structures, the structure members are prefixed with an abbreviated version of the structure such as:
struct passwd {
char *pw_name;
char *pw_passwd;
uid_t ...
0
votes
3
answers
210
views
How to implement state machine side effect that needs data the state machine doesn't have
Given a state machine that implements a certain protocol. I need multiple instances of this protocol running, so I create multiple instances of this state machine. Each instance is associated with a ...
3
votes
2
answers
278
views
Standard error codes vs custom error codes in C
I am working on improving the code quality and portability of my C library, specifically a ring buffer implementation, that will be used in larger applications. I have encountered a dilemma regarding ...
3
votes
1
answer
383
views
Is C usually a last resort?
I've been learning C recently.
I've completed a number of coding challenges on websites like codewars in C, and I always find myself wishing I had something like Python's flexible data structures. In ...
4
votes
2
answers
534
views
Why does C not support direct array assignment?
In C you cannot assign arrays directly.
int array[4] = {1, 2, 3, 4};
int array_prime[4] = array; // Error
At first I thought this might because the C facilities were supposed to be implementable with ...
0
votes
2
answers
142
views
Where to put the code that searches objects in a complex hierarchy?
Given the following hierarchy of objects: a keyed collection of ClassA objects, where each ClassA object contains a keyed collection of ClassB objects and each ClassB object contains a keyed ...
0
votes
1
answer
73
views
Avoiding repeating code when assigning pointer addresses based on branch
I have the following code in C that reads data off a char array and makes decisions on what variables should some pointers point to.
char preset1[20];
char preset1Name[20];
int preset1Temp;
int ...
2
votes
1
answer
650
views
Saving and reading files in user specific application data in C or C++
Most useful or reasonably complex applications need to save data such as user settings or saved games or browser history. I have been working on applications and games in C or C++ but I am not sure ...
-1
votes
2
answers
267
views
what algorithm does the malloc function in the c language standard library use? And why it so fast?
Based on the data structure of the AVL tree, I implemented a memory manager that does the best matching according to the size. I originally thought that the speed would be fast and the length of the ...
0
votes
1
answer
198
views
Is it true that variable type before name makes compiler work easier? [closed]
I have seen information that at least one of reasons why type placed before variable name is that it allows compiler easier evaluate size and type of variable. If so then how (what way) it eases this ...
-3
votes
1
answer
183
views
Will anything that's written in c/c++ be rerwritten in rust? [closed]
Will anything that's written in c/c++ be rerwritten in rust?
for maintainance, even for performance?
0
votes
1
answer
93
views
Is it safe to make training data and labels as global variables in C?
I'm trying to make this function called walk in C. The idea is that the function takes as parameters a point in space (represented by an array), a function (pointer), and a step size / learning rate ...
0
votes
2
answers
91
views
Where to check preconditions in multi functions
In a library, there could exist three types of functions. The first are those which are visible to the user i.e. their declarations are installed in the library's include directory. The third are ...
0
votes
1
answer
257
views
Custom #pragma directives
I'm creating a language parser on a microprocessor in C++. For the tables of keywords and commands, rather than maintaining a single curated file (alphabetically sorted, etc), I'd prefer to declare ...
-1
votes
1
answer
165
views
Better way to represent grammar symbols in C
I'm trying to build a simple compiler for a subset of the C language in C. To achieve this, I needed to figure out a way to represent the grammar symbols. Basically, each symbol can either be a "...
-3
votes
1
answer
175
views
When has C or C++ made itself intentionally backwards incompatible? [closed]
Context ( You can skip )
I will be doing a presentation on Python, and will be touching upon V3's decision to be intentionally backwards incompatible, and how that has affected Python long term. I ...
1
vote
1
answer
133
views
How to implement a RegEx library in C [closed]
Background
I have been attempting to create a line editor in C, eventually becoming a visual editor, and as a means of both allowing the editor to be useful but also learn more about C development, I ...
0
votes
2
answers
122
views
Unit testing 'opaque' classes / structs
I'm sorry, no good title.
I'm working on a C project right now and am writing tests. I usually do a 'struct method' style of programming. So I have opaque structs (no implementation revealed outside ...
0
votes
2
answers
270
views
How to create an interface in C that can work on two identical structs with differently named fields
Question Background
Consider a scenario in which I have two structs. Both consist of three fields for doubles. The only difference is the names used to refer to these fields. The first struct is for ...
4
votes
2
answers
279
views
Term for type information from C Source Code
I'm writing a code that extracts information about functions (prototype) and types (enum, struct, typedef) from a C source file.
This is to augment the type information about a particular function or ...
0
votes
2
answers
172
views
Symbolic constants versus variables
coders. I'm learning C programming language and found out such thing as #define directive that allows us to use symbolic constants (following Brain Kernigan). I know that this directives "insert&...
2
votes
6
answers
224
views
Initialize minimum value to INT_MAX or to first scanf call
I am finding the minimum value being input from stdin. For example,
1
5
3
2
-6
7
should return -6 to stdout.
My question is if it is a better practice to initialize my minimum value to INT_MAX or to ...
0
votes
4
answers
622
views
Is API exactly the same as header file?
I am trying to understand the difference between an Application Programming Interface and a Library and I stumbled upon this question:
https://stackoverflow.com/questions/3678665/is-there-still-a-...
3
votes
1
answer
1k
views
Using a preprocessor macro to wrap functions?
I have multiple use cases where I need to call an "entry" function, do a bunch of stuff, and then call an "exit" function. (In my case, "entry" is enabling a chip select ...
6
votes
4
answers
1k
views
What was the original purpose of C strncpy() function?
C standard library has strncpy function, declared as:
char *strncpy(char *dest, const char *src, size_t n);
It's a strange beast, as it fills n bytes of memory pointed to by dest. It does this by ...
0
votes
2
answers
88
views
How to tell client if predicate function fails?
Just say I have list APIs, and provide find() to search node.
With this design if something went wrong during pre-conditions, client would have no idea about it because no status code provided.
bool ...
-2
votes
1
answer
281
views
C (Arduino): treat a #defined hex value as ASCII chars [closed]
This is Arduino code, but since it uses C syntax, and it's not Arduino-specific, I thought I'd ask here. I have a few values defined so:
#define PID_RPM 0x0C
Typically it's used as a hex number. But ...
-1
votes
1
answer
133
views
Writing a API for a hardware device for mutliple platforms
We want to create an API to communicate witha device we currently sell.
The API should be available for several platforms like C / C++ / .NET / Python and available for Windows and Linux.
The idea is ...
0
votes
1
answer
164
views
In C and C++, can a compiler store an unamed object generated from a expression, if that same expression is reused, but the result is unchanged? [closed]
I'm mostly interested in C and C++, but I think this question can also apply to other languages.
My question is, if an unamed object or value is generated from the evaluation of an expression or ...
16
votes
5
answers
7k
views
Why are variables declared without a value in C?
The way I see used most frequently
int main()
{
int i;
for (i=0; i<10; i++)
{
printf("hello\n");
}
return 0;
}
The way I’m used to
int main()
{
...
0
votes
1
answer
586
views
How can I unit test state machines in C
In an 8bit mcu, I wrote a piece of code of a state machine using a function pointer static void (*state)(void);.
Inside the same compilation (let's say sm.c) unit I implement all the available states.
...
4
votes
4
answers
484
views
Design pattern to create a shared lib architecture
I am making a renderer as a hobby, one thing I thought to try is making the low levelAPI be dynamically swappable, i.e. you could have an opengl or vulkan backend and switch between the two without ...
2
votes
1
answer
279
views
Wrapping a legacy C++ codebase with a C API
I have inherited a large C++ codebase implementing various Windows desktop applications, services and libraries using Windows MFC. There are no automated tests. We need to decouple the UI and retain a ...
2
votes
2
answers
663
views
Strategy for offering a library both a single-header and as a header + compiled implementation
I am maintaining a FOSS library which, for the sake of discussion, consists of a small .h file and a larger .c file (plus build-related files, see below)
I've been requested to offer this library as a ...
-2
votes
2
answers
509
views
Is there any other reason to Null-terminate a string besides to find out the length of the string? [closed]
I have looked at the source code of some standard C functions and wondered what the reason is for null-terminating a string other than to find out its length.
e.g.
The strlcat() function which is ...
24
votes
8
answers
6k
views
Should I assume data passed to my function is accurate?
Assuming I have the following struct (just an example)
struct string{
int len;
char*str;
}
And I have the function
int init_str(struct string*s, int len);
which will perform
s->len=len;
s-...
0
votes
1
answer
269
views
Implement Dependency Inversion in C with UML diagram
As per Robert C. Martin in Clean Architecture, he gives a simple UML diagram to illustrate Dependency Inversion.
To put it simply, HL1 initially referred to ML1 without interface to invoke F() ...