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

learn more… | top users | synonyms

1
vote
1answer
60 views

When is an object passed to a function?

Trying to clearly state the semantics of a function call. In calling a function, are the arguments passed to the function the ones the calling code initially gives or the ones the function ...
14
votes
5answers
1k views

Why use an opaque “handle” that requires casting in a public API rather than a typesafe struct pointer?

I'm evaluating a library whose public API currently looks like this: libengine.h /* Handle, used for all APIs */ typedef size_t enh; /* Create new engine instance; result returned in handle */ ...
8
votes
1answer
193 views

Why are C string literals read-only?

What advantage(s) of string literals being read-only justify(-ies/-ied) the: Yet another way to shoot yourself in the foot char *foo = "bar"; foo[0] = 'd'; /* SEGFAULT */ Inability to elegantly ...
1
vote
3answers
77 views

Are file-scope `static` variables in C as bad as `extern` global variables?

In C, you'd often/sometimes (as a matter of style) use a file-scope static variable where you'd use a private class member variable in C++. When scaling to multithreaded programs, simply adding ...
0
votes
1answer
41 views

What is the most efficient way to discover devices on a network?

I've been programming a C web server from scratch as an interface for controlling ESP8266 WiFi modules that I plan to use to control appliances & lighting with relays. In an attempt to make a ...
-1
votes
0answers
31 views

Different variations of C [closed]

I've heard from a few sources that C is not too portable, because it is so close to the machine compared to higher level languages like Perl, Python, Java, etc. But, I was wondering how many ...
-3
votes
0answers
40 views

Algorithm for base conversion of arbitrary length number [closed]

Is there an efficient algorithm to convert arbitrary length number (stored as an array of digits) from one base to another? For example, base-10 number {3, 1, 9} to base-9 number {3, 8, 4}? I can ...
4
votes
5answers
670 views

Checking array size in C/C++ to avoid segmentation faults

So it's well known that C does not have any array bounds checking when accessing memory. Nowadays, if you call myArray[7] when you initialised it as int myArray[3], your program will get a segfault ...
4
votes
0answers
70 views

How to abstract from a display?

I'm building an embedded text editor consisting of a keyboard, an LCD display and a PIC32 microcontroller, to be programmed in C. The application should look, for example, like the GNU nano editor. ...
0
votes
0answers
3 views

Beginner in C needing help with strings and memory [migrated]

So this course I'm doing wants us to play around with memory management and pointers. I'm not really fully understanding them. I keep getting a "Segmentation fault (Core dumped)" error. Apparently I ...
1
vote
3answers
424 views

C: “this” vs “<instance name>”

I'm using C in a minimal, efficient OOP style to build a game engine. A problem I've begun to face as the engine code settles is my (erstwhile) choice to use this. For example, I have: void ...
3
votes
0answers
81 views

How to define type-less string of 1-bits [migrated]

I'm writing code for a flash driver and I sometimes have to check whether or not certain locations are empty i.e. contain 0xff. Sometimes I check bytes, sometimes longer values. So I wanted to have a ...
1
vote
0answers
30 views

C decompress Bitmask source [migrated]

This may be somewhat of an odd question as well as my first one ever on this site and a pretty complicated thing to ask basically I have this decompresser for a very specific archived file, I barely ...
0
votes
2answers
77 views

Proper way to interpret this dereference operation?

I've seen this example in a text book and am a little confused how to interpret the operator precedence rules. Given this struct: typedef struct { char *data; size_t start, end; } ...
53
votes
13answers
6k views

Are data type declarators like “int” and “char” stored in RAM when a C program executes?

When a C program is running, the data is stored on the heap or the stack. The values are stored in RAM addresses. But what about the type indicators (e.g., int or char)? Are they also stored? ...
-2
votes
1answer
60 views

What's an effective threading structure for NIO and server performance in C/C++? [closed]

I run into this issue where if I do a read to check for data too often, my CPU skyrockets, and if I don't check often enough, it's slow for client performance. What's an effective way to deal with ...
2
votes
0answers
73 views

Best practice for documenting ports/hardware for embedded project? [migrated]

I've recently taken an interest in embedded development and am wondering how most people document their projects. For example: Each pin on PORTE is wired to an LED and is aliased as LEDPORT. ...
5
votes
4answers
311 views

When did Undefined Behavior in C jump the causality barrier

Some hyper-modern C compilers will infer that if a program will invoke Undefined Behavior when given certain inputs, such inputs will never be received. Consequently, any code which would be ...
0
votes
0answers
49 views

Using the readahead syscall in C in the linux environment [migrated]

I am interested in using the readahead syscall in a c program. Currently I have opened a file for reading with a call to open(). Once I find a character, say the letter 'b', I want to test whether the ...
2
votes
6answers
727 views

Replacing C with JavaScript as an introductory programming language [closed]

I am a new teacher at polytechnic where we teach web development and basic software programming. For years, the institution where I teach have taught C as their introductory programming language to ...
0
votes
0answers
55 views

A callback for when data is received?

So, my understanding is that the Kernel could receive TCP at any time, and that generally a C program asks the Kernel synchronously/asynchronously if it has data available. Would it be possible for a ...
1
vote
1answer
155 views

What would be a reason not to embed JS in C? [closed]

I'm working on an opensource application that processes JSON in C, because I would like to make it easier for people to help me in the project and because JSON is so close to JS. I want to embed a JS ...
1
vote
2answers
63 views

What should JITed bytecode do exactly?

I'm working on a VM (and a scripting language for it) that I plan to implement JITing for. I'm only working on the "plumbing" of it now, but I don't want the JIT compiler to be an afterthought. ...
2
votes
1answer
145 views

Endianness at bit level

I am learning the union and struct and I wrote the code below. What I do not understand is why the output is different when I change from a little endian to a big endian machine. My understanding is ...
2
votes
1answer
150 views

What is the motivation for casting a pointer into a integer?

I'm doing some changes in the Linux kernel code and have noticed a pointer being cast into integer. Check out buf below (full code): snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream ...
2
votes
2answers
156 views

Why dynamic memory allocation functions in C returns void*?

Consider the prototypes of C's dynamic allocation functions malloc - void* malloc(size_t size); calloc - void* calloc(size_t n,size); realloc - void* realloc(void* ptr,size_t newsize); Now a ...
27
votes
5answers
2k views

Should I initialize C structs via parameter, or by return value? [closed]

The company I work at is initializing all of their data structures through an initialize function like so: //the structure typedef struct{ int a,b,c; } Foo; //the initialize function ...
1
vote
2answers
82 views

Multi-pattern matching

I have a given byte array in C and I need to match it against several byte arrays and return true if there is any match. I can make various memcmp's but I feel that it is very inefficient. Do you ...
2
votes
0answers
243 views

Fast fixed-size (256 bit) integer modulo/division

What is the fastest way to divide two 256-bit integers? They are represented as an array of 26-bit words, each stored in 32-bit integers. I have access to a 64-bit type if that's useful. It seems ...
0
votes
2answers
171 views

In C, how are functions accessible if they're not inline or called by #include? [closed]

I'm looking at code that somehow calls a library but I don't see it calling any of the library's headers at any point. I've done searches within the code and I don't see anywhere the functions, ...
-1
votes
2answers
138 views

How does the post increment operator work in GNU C? [closed]

While the run the below program in Turbo C compiler, I am getting the expected output, however, when I run the same program it using "gcc" compiler in linux, it is giving an unexpected output! int ...
1
vote
3answers
209 views

What does the “t” in int32_t signify?

In C, what meaning, if any does the t at the end of integer types like uint8_t and int32_t have? Where did it originate? Why wasn't the type just called int32?
12
votes
1answer
360 views

Why using sizeof of type to calculate the number of elements in an array is considered inferior to using sizeof of an element?

I'm reading "King K.N's C programming" and I'm reading the following statement: We discussed using the expression sizeof(a)/sizeof(a[0]) to calculate the number of elements in an array. The ...
1
vote
3answers
115 views

How much can you detect undefined behaviour using testing [closed]

I hope this question fits this site. You may know you can't detect undefined behaviour in C using compilers - and some tools (static analysis) can help you detect it. My question is more empirical - I ...
1
vote
1answer
214 views

Inserting C++ code into C [closed]

What I need to do before inserting C++ code into C, and how to do it? Are there any examples to show me how this can be done. I have a good knowledge of C, but never mixed those two. Thanks for the ...
0
votes
1answer
102 views

floating-point number stored in float variable has other value [duplicate]

I'm reading a book about C programming By "King.K.N" and I'm reading the following statement: If we store 0.1 in a float variable, we may later find that the variable has a value such as ...
3
votes
1answer
338 views

How do C/C++ programs get ported to different platforms? [closed]

Java applications can run on different platforms because they always run on top of a Virtual Machine specifically developed for each platform. So, there's no native installation, you just drop the ...
0
votes
2answers
110 views

What's the simplest way to deal with events in C in this situation?

I'm writing a text-based game, in C. I want the game to handle NPCs movement and actions in the background, while the user is making his moves. I'd also use time-based events to tell the user when ...
9
votes
5answers
2k views

Simple and clean way of comparing three numbers

I've got some code that has a sequence of ifs that work, but just feel messy. Basically, I want to choose the largest of three integers and set a status flag to say which was chosen. My current code ...
1
vote
1answer
76 views

Best practices for tracking multiple pieces of data in a program

I have a project in C (don't ask why but it needs to be in C), where I need to track multiple pieces of data and commands for different modules. Some actions that the program will take depend on the ...
2
votes
1answer
73 views

Are there any problems with defining a single-source “module” in C using conditional compilation to separate the “header” from “source”

I'm writing a project that I want to keep small in the sense of being very densely-coded and a single source file. But it's growing large enough that navigating the file is becoming tiresome, so I ...
0
votes
2answers
86 views

Multiple threads making call to precompiled library

I have been asked this question in an interview and I thought to take advice from you guys. Say I have a pre-compiled library which references to some function say "strtok". There is no source code ...
2
votes
2answers
108 views

What is the point of designing Lua C APIs?

I am a newbie of Lua. After studying some Lua C APIs examples, I am a little confused. I can see the Lua C API is used for processing Lua scripts: ...... lua_State *L = luaL_newstate(); ...
1
vote
2answers
132 views

Complex iterators in C

note: this was originally asked on SO. Part of my current project involves iterating over sequences that don't necessarily exist. For example, I have something similar to a relational database in ...
16
votes
2answers
362 views

Has variable width types been replaced by fixed types in modern C?

I came across an interesting point today in a review over on Code Review. @Veedrac recommened in this answer that variable size types (e.g. int and long) be replaced with fixed size types like ...
3
votes
3answers
148 views

How to maintain modularity in C?

I am writing C code with multiple modules like LCD display, flash memory, and GSM module etc. The project consists of thousands of lines of code, in different files. The behavior of the system can be ...
6
votes
2answers
1k views

What is the difference between function() and function(void)?

I have heard that it is a good practice to write functions that do not receive anything as a parameter like this: int func(void); But I hear that the right way to express that is like this: int ...
-1
votes
2answers
47 views

If I #include a file, do I need to have a valid path to any headers #included in the included file [closed]

For example, if I have created a library, libcommon, which uses some other custom, but widely used library in a specific field (some_other), like this: libcommon.h #ifndef LIBCOMMON_H #define ...
26
votes
10answers
6k views

What is the “type” of data that pointers hold in the C language?

I know that pointers hold addresses. I know that pointers' types are "generally" known based on the "type" of data they point to. But, pointers are still variables and the addresses they hold must ...
1
vote
0answers
334 views

Why C doesn't have better notation for pointers? [duplicate]

The * symbol is used for three different purposes multiplication Pointer declaration Pointer dereferencing. Why designers of C kept this notation? In other words, wouldn't it be nice if there ...