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
11
votes
4answers
5k views

C project avoiding naming conflicts

I'm struggling to find pragmatic real-world advice on function naming conventions for a medium sized C library project. My library project is separated into a few modules and submodules with their own ...
1
vote
3answers
168 views

Does the gcc optimize out local arrays the same way it would optimize out a local variable?

If I write this code in a function: void func(void) { int x = 0; int y = 3724832+x; } It would (probably) optimize the variable y out because it isn't being used. Even if I'm wrong about ...
181
votes
8answers
55k views

Is every language written in C?

Sometimes while programming in different languages (C/C++, C#), this thought comes to my mind: Is each and every language written in the C programming language? Is the C language the mother/father ...
5
votes
1answer
6k 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 *...
0
votes
1answer
40 views

Thread-safe C library with OpenMP

I am writing a C library with routines I want to call from an external driver program using many OpenMP threads. The library will have a main container called lib_workspace, which will be used to do ...
-1
votes
2answers
121 views

malloc in caller, free in callee

Is it good practice to malloc in the caller function, and then free in the callee function? Example: int main() { int *ptr = malloc(100); foo(ptr); return 0; } void foo(int *ptr) { ...
2
votes
3answers
203 views

Practical way to indicate an error from a function that returns a stuct instance?

Suppose I have a function that returns an instance of a struct with the following declaration: struct data API(void); Suppose also that there is a chance for the function to fail with an error ...
2
votes
1answer
1k views

How does a dynamic library's references to a global variable get translated once in the running app?

If a dynamic library exports the address to a global variable defined within the library, how are accesses to that variable translated during dynamic linking so that a running application can interact ...
0
votes
1answer
86 views

C Function returning pointer to first element vs. a pointer to an entire array

Suppose I have an API function which returns a pointer to some memory, that is later to be consumed in an array-like manner. Suppose also that its size is known in advance, and that it's declared like ...
1
vote
1answer
123 views

Switching out implementation source files in C project

For simplicity, let's say I have the following C project structure: src/ utils/ logger.c logger.h main.c secondary_component.c main.c starts with: #include "utils/logger.h" I would ...
0
votes
1answer
78 views

Sharing #define's between higher and lower layer. Where should shared types go? [closed]

Let's say I have a stacked 2 layer app (High Layer (HL) and Low Layer (LL)) that is implemented in C. HL defines a few #defines. HL calls a LL function with a parameter that takes values of the #...
0
votes
1answer
141 views

Should “NaN” default to SNaN or QNaN

I created a function to read floats (IEEE 754 binary 32-bit) from strings, for developers. I have everything finished and working perfectly, except i'm wondering if a user/or programmer enters a ...
0
votes
2answers
464 views

How to prevent 'global variables' in a big project?

With 'global variables', I mean Variables on namespace level Static data members in classes Static variables in functions In a big C++ project I would like to have a mechanism (like a compiler ...
1
vote
2answers
105 views

Reading problem of object-oriented concept about the book “Object-oriented software construction 2/e”

I've been learning object-oriented concept from this book, and stuck in a specified concept in the middle of chapter 6. Because with no brief explaining beforehand, no one could understand my ...
2
votes
5answers
469 views

Advantages/Disadvantages of Compiling as Both C and C++?

So I've seen some C libraries written so that they compile without any changes with a C++ compiler, for example Lua. What are some advantages and disadvantages of doing this? A few basic ones I see ...
1
vote
0answers
24 views

MPI Derived Types and Portability

When using derived types in MPI for communication of data which is not contiguous in memory, the sequence of datatype-displacement pairs defining the derived type seems to be assumed to be the same ...
2
votes
2answers
289 views

A way not to forget to check for errors in C

As everybody knows, C allows us to write anything we want. There comes one big problem — we are the only who responsible to handle errors that comes from standard library functions and, therefore, ...
7
votes
5answers
18k views

Is it possible to write an operating system in C? [closed]

A while back I saw a comment on a Stack Overflow question that surprised me: No one can write an operating system in C. It is not possible. What one can do is write an operating system in a C-...
95
votes
14answers
181k views

Is the C programming language still used?

I am a C# programmer, and most of my development is for websites along with a few Windows applications. As far as C goes, I haven't used it in a long time, as there was no need to. It came to me as a ...
0
votes
0answers
33 views

How to analyse the throughput of multithreaded client server programs?

I am practicing socket programming in C language. I have two codes - server and client (based on TCP) on two different laptops. The server forks a new process for every new request. For simulating ...
207
votes
5answers
262k views

How to write a very basic compiler

Advanced compilers like gcc compile codes into machine readable files according to the language in which the code has been written (e.g. C, C++, etc). In fact, they interpret the meaning of each codes ...
18
votes
7answers
40k views

Why do we need to include the .h while everything works when including only the .cpp file?

Why do we need to include both the .h and .cpp files while we can make it work solely by including the .cpp file? For example: creating a file.h containing declarations, then creating a file.cpp ...
1
vote
1answer
121 views

How float data type stores number greater than 2^23? As Mantissa is only 23 bits

Can someone explain how floating point numbers are stored in C, as for 32-bit float, 1 bit is parity, 23 bits for mantissa and 8 bits for exponent. So for numbers greater than 2^23(still in the float ...
1
vote
2answers
3k views

Why use SDL and OpenGL instead of just OpenGL?

I've been seeing people combine SDL2 and OpenGL (glfw.h or glut.h) for a while now, I've done some research and found out SDL2 runs on OpenGL. So why does people combine these two? Some people ...
2
votes
1answer
114 views

How are primitives stored in memory

In my searching although this question seems to get answered a lot, the answers don't really cover what I want to ask. I'm learning assembly and my question is, in higher level languages e.g C data ...
3
votes
2answers
300 views

What is the difference between a static library and an archive library?

In the comments of a recent answer, I equated a static library with an archive of compiled object files. The response was that they are not the same, so what is the difference? To clarify - gcc ...
7
votes
9answers
3k views

Is it any good to use binary arithmetic in a C++ code like “C style”?

I like the fact that the C language lets you use binary arithmetic in an explicit way in your code, sometimes the use of the binary arithmetic can also give you a little edge in terms of performance; ...
0
votes
1answer
70 views

Types of errors that the primitive operators in C might be involved in

I am new to C and wondering how to properly build primitive functions in order to handle errors. Take for example the following function: bool testIfEqual(int a, int b) { return a == b; } ...
1
vote
0answers
3k views

Best way to signal all child processes to terminate using C

I have been assigned the task of designing a simple web server using C in my UNIX & C class. The webserver listens and forks when a successful connection is made so that multiple clients may be ...
1
vote
1answer
154 views

Will Windows handle any memory not freed as well as threads not closed? [closed]

If I had an application that pretty much had a "fail-safe" as in, if I need to terminate the application quickly and not free the malloc'd memory, or close the threads that are running, would Windows ...
60
votes
7answers
10k views

Should one check for every little error in C?

As a good programmer one should write robust codes that will handle every single outcome of his program. However, almost all functions from the C library will return 0 or -1 or NULL when there's an ...
2
votes
1answer
46 views

Architecture design for Linux administrative interface

I've developed an IoT device utilising OpenWrt so my design needs run a on a shoe-string budget of resources. I need to be able to push small amounts of data, up to 300bytes to my app on a very ...
-2
votes
2answers
107 views

Should I call a callback for an event triggered by a developer?

I'm designing an API for a stream-processing library. The library will process a stream provided by the user and will transition between states based on the data in the stream. The state transitions ...
2
votes
1answer
296 views

Why did Java and C sharply dip in popularity around 2017 in the TIOBE index? [closed]

The TIOBE Programming Community Index shows Java and C dramatically falling in popularity between late 2016 and mid 2017 before rebounding by mid 2018. What is the cause of the volatility? Source: ...
2
votes
1answer
441 views

Embedded software architecture - What is the difference between DAL and HAL?

I'd like to know what the difference is between hardware abstraction layer (HAL) and data abstraction layer (DAL) when speaking about embedded software. I might have the abbreviations wrong though... ...
11
votes
4answers
2k views

Evolution of the Win32 API [closed]

Has the Windows 32 API changed much over time? Does the Charles Petzold book "Programming Windows, 5th Edition" from 1998 still contain relevant information, or is most of it obsolete/outdated? What ...
89
votes
16answers
18k views

Do the young minds need to learn the pointer concepts?

Why did the C master Dennis Ritchie introduce pointers in C? And why did the other programming languages like VB.NET or Java or C# eliminate them? I have found some points in Google, and I want to ...
11
votes
3answers
62k views

How are negative signed values stored?

I was watching this video on the maximum and minimum values of signed integers. Take an example of a positive signed value - 0000 0001 The first bit denotes that the number is positive and the last ...
3
votes
3answers
488 views

How does a compiler work when it's not directly compiling to machine code

I know the compilation process goes with this flow: source -> parse -> AST -> intermediate code -> assembly -> machine code and in the case of Java you will have bytecode which is ...
0
votes
1answer
118 views

Is it possible to swap out programs displayed in stdout?

I would like to write a program in C that forks then execs three different processes. Afterwards, two of the programs will be suspended and only one of the three will be outputting to stdout. Is is ...
0
votes
2answers
55 views

Architecture/design for reading incoming data and distributing it across program

This may be a broad question but I have not been able to find an answer. In my program I am communicating to a device over serial communication. The data coming through is binary, and formatted with ...
2
votes
3answers
436 views

GCC or Clang to output bytecode for a VM?

Long story short, I wanted to use C as a scripting language and in a portable manner so I created a register-based, JITless VM to accomplish this. I've formalized the VM's ISA, script file format, ...
1
vote
3answers
187 views

Does each process have it's own section of data, text , stack and heap in the memory?

I have created the above picture to illustrate my question. Is there a section within memory (lets say from address 0x1 to 0x15) that all processes use to place their text segment in (left figure), ...
-1
votes
1answer
135 views

static, dynamic analysis - what mistakes were made in the code?

A software company develops software packages for commercial animal farming. A special function in C calculates the daily amount of feed for different kind of animals dependent on their bodyweight....
46
votes
3answers
10k views

Why do we still grow the stack backwards?

When compiling C code and looking at assembly, it all has the stack grow backwards like this: _main: pushq %rbp movl $5, -4(%rbp) popq %rbp ret -4(%rbp) - does this mean the ...
33
votes
5answers
19k 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 ...
5
votes
2answers
291 views

Why use multi-purpose getters that take an enum value, rather than separate getters?

I'm designing a C API which will have about a dozen getter functions for various values. Something like: bool getSomeBool(); bool getSomeOtherBool(); ... int getSomeNumber(); int getSomeOtherNumber();...
34
votes
8answers
12k views

Why does Clang/LLVM warn me about using default in a switch statement where all enumerated cases are covered?

Consider the following enum and switch statement: typedef enum { MaskValueUno, MaskValueDos } testingMask; void myFunction(testingMask theMask) { switch (theMask) { case ...
0
votes
1answer
88 views

Chain function calls which return error codes or 0 on success in C [closed]

I used to call functions which returns int error code or 0 on success like this: int tmp = function_a() ?: function_b() ?: function_c(); if (tmp) handle_error(); Now I'm ...
0
votes
2answers
192 views

How can I figure out where the code starts for a complex software project? [duplicate]

Say I want to (try to) read through and understand a fairly complex piece of code (for example the free software Coreboot firmware code, which can be found here). How can I figure out where the code ...