Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [c]

C is a general-purpose computer programming language used for operating systems, games and other high performance work.

Filter by
Sorted by
Tagged with
4 votes
4 answers
950 views

Is updating a macro value in Xcode preprocessors marcos violating open closed principle?

For example, for some xcode projects, if I have some places that defines a number at some .cpp files: const int PAGE_MAX=5; and a new requirement comes that needs to change PAGE_MAX, I need to modify ...
wcminipgasker2023's user avatar
2 votes
3 answers
268 views

How would I go about writing my own implementation of Win32 functions?

So I am currently coding a C program for Windows and come across a little bit of a problem. I've been compiling using the mingw-w64 toolchain. In my program, I am attempting to remove as many ...
baron's user avatar
  • 55
0 votes
1 answer
107 views

Elegant way in C to store many parameters with default value and current value in embedded flash

I'm programming an embedded system that has a number of user configurable parameters, which are stored in flash memory. I have to store a default value for each parameter as well as the user settings. ...
jusaca's user avatar
  • 175
2 votes
1 answer
206 views

C++ vs C console output idioms

I'm struggling to understand pros and cons of the C++ and C approaches to console output. C++ uses the stream approach with concatenation of operator<<, while for "C approach" I mean a ...
Nicola Mori's user avatar
0 votes
2 answers
169 views

How might encapsulated source be broken into into multiple files?

I have a project where there is a primary, high-level, opaque struct with many functions that operate on it. Maybe I am simulating a CPU. How might the corresponding source code be organized? One way ...
Ana Nimbus's user avatar
1 vote
2 answers
103 views

How to handle errors of pthreads fundamental lock und unlock functions?

I am writing a little C library for the Raspberry Pi for controlling 433MHz transmitters/receivers. While receiving data the whole application would block, so I decided to put this code into a ...
blackdog's user avatar
25 votes
2 answers
6k views

Why is the term "string" so often abbreviated as "sz"?

A pattern I have noticed in many big C and C++ programs - including Microsoft Windows (REG_SZ type in Registry) and Valve's Source SDK (names of practically every string variable) - is that "sz&...
AcinonX's user avatar
  • 613
3 votes
2 answers
520 views

Maintaining global states in a recursive function

I am writing a recursive function. Some of the parameter data is different for each recursive call but some of the data only need to have one copy existing at any one time during the recursive call. ...
CPlus's user avatar
  • 1,187
0 votes
0 answers
54 views

Are type, constant/static/global, and pointer prefixes still necessary in C coding standards, considering the capabilities of modern IDEs? [duplicate]

Our coding standards for C include various prefixes for data types, constants, static/global variables, and pointers. These prefixes were originally introduced for code review purposes, but with the ...
Cem Polat's user avatar
  • 127
21 votes
4 answers
9k views

In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?

I have a situation like the following: int fds[2]; pipe(fds); // Write lots of stuff into fds[1] close(fds[1]); do_stuff(fds[0]); // Should I close fds[0] here or at the end of do_stuff? Which one ...
eem's user avatar
  • 329
10 votes
5 answers
11k views

Why is int in C in practice at least a 32 bit type today, despite it being developed on/for the PDP-11, a 16 bit machine?

For background, the question is to prepare some training material, which should also should explain a bit why the things are the way they are. I tried to get some idea of how C began based on this ...
Torsten Knodt's user avatar
1 vote
1 answer
545 views

Linux poll with a thread pool and multiple events

I am working on a simple client server program in C in which multiple clients will be connected to a single server. Clients will submit operations/actions to the server and the server will process ...
nick2225's user avatar
  • 147
1 vote
3 answers
208 views

How robust should an interface/implementation be?

In a spare-time project of mine, I implemented RSA public-key cryptosystem. Because the official PKCS#1 standard specify key formats in terms of ASN.1 syntax and DER/BER coding, which is a coding with ...
DannyNiu's user avatar
  • 219
0 votes
1 answer
139 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 ...
Severo Raz's user avatar
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 ...
CPlus's user avatar
  • 1,187
20 votes
5 answers
14k 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 ...
euraad's user avatar
  • 325
0 votes
1 answer
153 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. ...
DannyNiu's user avatar
  • 219
1 vote
1 answer
178 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"...
Patrick Wright's user avatar
0 votes
3 answers
1k 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 ...
Richard H. Nguyen's user avatar
4 votes
1 answer
303 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 ...
Infinite_Maelstrom's user avatar
-1 votes
1 answer
104 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 (...
Marco's user avatar
  • 377
1 vote
4 answers
558 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 ...
Sam's user avatar
  • 31
1 vote
3 answers
209 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 ...
jrgilman's user avatar
  • 121
1 vote
0 answers
85 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 ...
Sam's user avatar
  • 31
4 votes
1 answer
323 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 ...
tsvallender's user avatar
0 votes
2 answers
234 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 *...
Kaz's user avatar
  • 3,622
0 votes
0 answers
40 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 ...
CPlus's user avatar
  • 1,187
0 votes
3 answers
519 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 ...
PieterV's user avatar
  • 215
3 votes
2 answers
988 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 ...
Usman Mehmood's user avatar
3 votes
1 answer
407 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 ...
Connor's user avatar
  • 159
5 votes
2 answers
1k 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 ...
CPlus's user avatar
  • 1,187
0 votes
2 answers
152 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 ...
PieterV's user avatar
  • 215
0 votes
1 answer
76 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 ...
HFOrangefish's user avatar
2 votes
1 answer
965 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 ...
CPlus's user avatar
  • 1,187
-1 votes
2 answers
347 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 ...
CukiPid's user avatar
  • 11
0 votes
1 answer
215 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 ...
Ya Y's user avatar
  • 29
-3 votes
1 answer
195 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?
Snowmanzzz's user avatar
0 votes
1 answer
98 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 ...
Mehdi Charife's user avatar
0 votes
2 answers
128 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 ...
Nitin Malapally's user avatar
0 votes
1 answer
465 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 ...
SteveG's user avatar
  • 33
-1 votes
1 answer
195 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 "...
Mehdi Charife's user avatar
-3 votes
1 answer
213 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 ...
Anon's user avatar
  • 3,613
1 vote
1 answer
337 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 ...
greybrunix's user avatar
0 votes
2 answers
244 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 ...
Derek C.'s user avatar
  • 617
0 votes
2 answers
431 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 ...
Shadow43375's user avatar
4 votes
2 answers
280 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 ...
Marco's user avatar
  • 377
0 votes
2 answers
226 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&...
CoderDesu's user avatar
  • 1,015
2 votes
6 answers
250 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 ...
Nicholas Ficara's user avatar
0 votes
4 answers
949 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-...
Noob_Guy's user avatar
  • 159
3 votes
1 answer
2k 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 ...
fearless_fool's user avatar

1
2 3 4 5
27