Sidebar

C Programming Language

c_lang
C Programming Language KindaABigDyl 4 weeks ago 73%
ACBS - Another C Build System

I created a little side project over the past few days, a new build system for C and C++: https://github.com/blueOkiris/acbs/ I've seen a lot of discourse over C build tools. None of them really seem solid except for (some) Makefiles (some Makefiles are atrocious; you just can't rely on people these days). Bazel, cmake - they're just not straight forward like a clean Makefile is, basically black magic, but setting up a Makefile from scratch is a skill. Many copy the same one over each time. Wouldn't it be nice if that Makefile didn't even need to be copied over? Building C should be straight forward. Grab the C files and headers I want, set some flags, include some libraries, build, link. Instead project build systems are way way way overcomplicated! Like have you ever tried building any of Google's C projects? Nearly impossible to figure out and integrate with projects. So I've designed a simplistic build system for C (also C++) that is basically set up to work like a normal Makefile with gcc but where you don't have to set it up each time. The only thing you are required to provide is the name of the binary (although you can override defaults for your project, and yes, not just binaries are possible but libs as well). It also includes things like delta building without needing to configure. Now there is one thing I haven't added yet - parallel building. It should be as simple as adding separate threads when building files (right now it's a for loop). I know that's something a lot of people will care about, but it's not there yet. It's also really intended to only work with Linux rn, but it could probably pretty easily be adjusted to work with Windows. Lay your project out like the minimal example, adjust the project layout, and get building! The project itself is actually bootstrapped and built using whatever the latest release is, so it's its own example haha. It's dead simple and obvious to the point I would claim that if your project can't work with this, your project is wrong and grossly over-complicated in its design, and you should rework the build system. C is simple, and so should the build system you use with it! So yeah. Check it out when y'all get a chance

9
6
c_lang
C Programming Language pnutzh4x0r 1 month ago 100%
Getting back into C programming for CP/M https://kevinboone.me/cpm-c.html

> This article is about developing in C for CP/M, using a 40-year-old C compiler, and how this differs from modern C development. The compiler I'm using is the 1982 release of Manx Software Systems' "Aztec C". The compiler is freely, and legally, available from the Aztec Museum. A lot of CP/M software falls into the broad category of "abandonware" -- software that is notionally still protected by intellectual propery law, but whose owners have no interest in it, or cannot even be identified.

16
2
c_lang
C Programming Language ericjmorey 1 month ago 100%
Writing a C Compiler | Build a Real Programming Language from Scratch | Nora Sandler | July 2024 | No Starch Press | 792 pages | ISBN-13: 9781718500426 nostarch.com

### Book Description Writing a C Compiler will take you step by step through the process of building your own compiler for a significant subset of C—no prior experience with compiler construction or assembly code needed. Once you’ve built a working compiler for the simplest C program, you’ll add new features chapter by chapter. The algorithms in the book are all in pseudocode, so you can implement your compiler in whatever language you like. Along the way, you’ll explore key concepts like: - Lexing and parsing: Learn how to write a lexer and recursive descent parser that transform C code into an abstract syntax tree. - Program analysis: Discover how to analyze a program to understand its behavior and detect errors. - Code generation: Learn how to translate C language constructs like arithmetic operations, function calls, and control-flow statements into x64 assembly code. - Optimization techniques: Improve performance with methods like constant folding, dead store elimination, and register allocation. Compilers aren’t terrifying beasts—and with help from this hands-on, accessible guide, you might even turn them into your friends for life. ### Author Bio Nora Sandler is a software engineer based in Seattle. She holds a BS in computer science from the University of Chicago, where she researched the implementation of parallel programming languages. More recently, she’s worked on domain-specific languages at an endpoint security company. You can find her blog on pranks, compilers, and other computer science topics at https://norasandler.com.

20
5
c_lang
C Programming Language t0mri 3 months ago 100%
fatal error: dbus/dbus-arch-deps.h: No such file or directory

cross-posted from: https://lemmy.ml/post/17545833 > im new to system programming, idk if thats the issuse. but according gcc, it can not find `dbus/dbus-arch-deps.h` and thats all i know. any idea about this issue? > > ``` > program > [I] tomri@artix ~ [1]> cat main.c > #include <dbus-1.0/dbus/dbus.h> > > int main(void) { return 0; } > ``` > ``` > # error > [I] tomri@artix ~> gcc main.c > In file included from main.c:1: > /usr/include/dbus-1.0/dbus/dbus.h:29:10: fatal error: dbus/dbus-arch-deps.h: No such file or directory > 29 | #include <dbus/dbus-arch-deps.h> > | ^~~~~~~~~~~~~~~~~~~~~~~ > compilation terminated. > ``` > ``` > [I] tomri@artix ~> ls -la /usr/include/dbus-1.0/dbus/ > total 216 > drwxr-xr-x 2 root root 4096 Jul 2 20:26 ./ > drwxr-xr-x 3 root root 4096 Jun 15 21:36 ../ > -rw-r--r-- 1 root root 2809 Jan 14 06:17 dbus-address.h > -rw-r--r-- 1 root root 3470 Jan 14 06:17 dbus-bus.h > -rw-r--r-- 1 root root 27018 Jan 14 06:17 dbus-connection.h > -rw-r--r-- 1 root root 2909 Jan 14 06:17 dbus-errors.h > -rw-r--r-- 1 root root 22076 Jun 8 2023 dbus-glib-bindings.h > -rw-r--r-- 1 root root 2575 Jun 8 2023 dbus-glib-lowlevel.h > -rw-r--r-- 1 root root 14766 Jun 8 2023 dbus-glib.h > -rw-r--r-- 1 root root 8969 Jun 8 2023 dbus-gtype-specialized.h > -rw-r--r-- 1 root root 1464 Jun 8 2023 dbus-gvalue-parse-variant.h > -rw-r--r-- 1 root root 7246 Jan 14 06:17 dbus-macros.h > -rw-r--r-- 1 root root 1961 Jan 14 06:17 dbus-memory.h > -rw-r--r-- 1 root root 15259 Jan 14 06:17 dbus-message.h > -rw-r--r-- 1 root root 1810 Jan 14 06:17 dbus-misc.h > -rw-r--r-- 1 root root 3809 Jan 14 06:17 dbus-pending-call.h > -rw-r--r-- 1 root root 23956 Jan 14 06:17 dbus-protocol.h > -rw-r--r-- 1 root root 5412 Jan 14 06:17 dbus-server.h > -rw-r--r-- 1 root root 5392 Jan 14 06:17 dbus-shared.h > -rw-r--r-- 1 root root 3047 Jan 14 06:17 dbus-signature.h > -rw-r--r-- 1 root root 2359 Jan 14 06:17 dbus-syntax.h > -rw-r--r-- 1 root root 8505 Jan 14 06:17 dbus-threads.h > -rw-r--r-- 1 root root 4143 Jan 14 06:17 dbus-types.h > -rw-r--r-- 1 root root 3961 Jan 14 06:17 dbus.h > [I] tomri@artix ~> > ``` > ``` > # my system > [I] tomri@artix ~> uname --all > Linux artix 6.9.7-artix1-1 #1 SMP PREEMPT_DYNAMIC Fri, 28 Jun 2024 18:11:28 +0000 x86_64 GNU/Linux > ```

6
3
c_lang
C Programming Language promitheas 6 months ago 100%
Blackjack game as a learning experience - Request for suggestions, ways to make it better, advice etc... github.com

Hello everyone :) I recently wrote a small blackjack game where the challenge I set myself (and the learning goal) was to use pointers to do stuff. So for example instead of having all my functions written in such a way as to avoid using pointers I would use them wherever possible. It works in its current form and seems to do so quite well, but I would like to learn even more from this project. So here I am asking for your experience. Please give me any suggestions, advice, ways to make it more efficient in terms of memory used, ways to make it more efficient in terms of speed (I know memory and speed might not be so important for a small project like this but my goal is to practice and learn the underlying concepts), criticisms, best practices, or anything you that comes to mind when you look at the code. As an example of what I mean, I would like to make it so that the arrays which hold the player and dealer hands are not of fixed size on declaration, but grow and shrink dynamically as cards are added and removed from them (practice memory allocation/deallocation). I hope I provided enough info about what I am looking for in a clear manner, but if not please do ask for clarification/additional info. Thanks in advance!

5
0
c_lang
C Programming Language modev 7 months ago 100%
Modern C, Third Edition (covers the C23 standard) https://gustedt.gitlabpages.inria.fr/modern-c/

Almost 50 years ago, the C language defined modern computer programming. This book shows you why C is still as powerful and popular as ever, with an inside look at the new C23 standard. For programs that need to be small, fast, and unfailingly reliable, C is still the gold standard. Whether you’re writing embedded code, low-level system routines, or high-performance applications, C is up to the challenge. This unique book by Jens Gustedt, a member of the ISO C standards committee, gets you up to speed with C23. In Modern C, Third Edition you’ll: - Learn C basics, core features, and advanced concepts - Leverage major C23 improvements for security, reliability, and performance - Write portable code that runs anywhere - Build multi-threaded applications with atomics and synchronization - Create robust and resilient software with error handling - Use type-generic programming for reusable code C powers more software than any other language — from embedded devices to distributed systems. In Modern C, Third Edition you’ll learn to harness C’s full potential using the latest tools and techniques. After a quick review of the fundamentals perfect for beginners or coders who haven’t used C in a while, this book guides you to mastery of C23, the latest ISO standard.

23
1
c_lang
C Programming Language modev 7 months ago 41%
What do you think about brains unsafety?

Maybe you have to hear about [this](https://www.tomshardware.com/software/security-software/white-house-urges-developers-to-avoid-c-and-c-use-memory-safe-programming-languages). What do you think? I think: ![C will never die](https://programming.dev/pictrs/image/2f2557de-7b0b-4a3c-a33b-cd72e71a1a14.jpeg) *For infrastructure technology, C will be hard to displace.* © Dennis Ritchie

-3
5
c_lang
C Programming Language modev 7 months ago 94%
Understanding and Using C Pointers

Developers struggle with C pointers because they do not feel confident. I found a very good book about it: **Understanding and Using C Pointers** by *Richard Reese*. "Why You Should Become Proficient with Pointers Pointers have several uses, including: - Creating fast and efficient code - Providing a convenient means for addressing many types of problems - Supporting dynamic memory allocation - Making expressions compact and succinct - Providing the ability to pass data structures by pointer without incurring a large overhead - Protecting data passed as a parameter to a function Faster and more efficient code can be written because pointers are closer to the hardware. That is, the compiler can more easily translate the operation into machine code. There is not as much overhead associated with pointers as might be present with other operators. Many data structures are more easily implemented using pointers. For example, a linked list could be supported using either arrays or pointers. However, pointers are easier to use and map directly to a next or previous link. An array implementation requires array indexes that are not as intuitive or as flexible as pointers." "A solid understanding of pointers and the ability to effectively use them separates a novice C programmer from a more experienced one. Pointers pervade the language and provide much of its flexibility. They provide important support for dynamic memory allocation, are closely tied to array notation, and, when used to point to functions, add another dimension to flow control in a program. Pointers have long been a stumbling block in learning C. The basic concept of a pointer is simple: it is a variable that stores the address of a memory location. The concept, however, quickly becomes complicated when we start applying pointer operators and try to discern their often cryptic notations. But this does not have to be the case. If we start simple and establish a firm foundation, then the advanced uses of pointers are not hard to follow and apply. The **key** to comprehending pointers is **understanding how memory is managed in a C program**. After all, pointers contain addresses in memory. If we don’t understand how memory is organized and managed, it is difficult to understand how pointers work. To address this concern, the organization of memory is illustrated whenever it is useful to explain a pointer concept. Once you have a firm grasp of memory and the ways it can be organized, understanding pointers becomes a lot easier." --- Good explanation about *"Differences Between Arrays and Pointers"* There are several differences between the use of arrays and the use of pointers to arrays. In this section, we will use the vector array and *pv* pointer as defined below: ```c int vector[5] = {1, 2, 3, 4, 5}; int *pv = vector; ``` The code generated by *vector\[i\]* is different from the code generated by *vector+i*. The notation *vector\[i\]* generates machine code that starts at location vector, **moves** *i* positions from this location, and uses its content. The notation *vector+i* generates machine code that starts at location *vector*, **adds** *i* to the address, and then uses the contents at that address. While the result is the same, the generated machine code is different. This difference is rarely of significance to most programmers. There is a difference when the *sizeof* operator is applied to an array and to a pointer to the same array. Applying the *sizeof* operator to *vector* will return 20, the number of bytes allocated to the array. Applying the *sizeof* operator against *pv* will return 4, the pointer’s size. The pointer *pv* is an *lvalue*. An *lvalue* denotes the term used on the lefthand side of an assignment operator. An *lvalue* must be capable of being modified. An array name such as *vector* is not an *lvalue* and cannot be modified. The address assigned to an array cannot be changed . A pointer can be assigned a new value and reference a different section of memory. Consider the following: ```c pv = pv + 1; vector = vector + 1; // Syntax error ``` We cannot modify *vector*, only its contents. However, the expression *vector+1* is fine, as demonstrated below: ```c pv = vector + 1; ```

17
0
c_lang
C Programming Language modev 8 months ago 80%
Ode to C

Personally, I have nothing against the emergence of new programming languages. This is cool: - the industry does not stand still - competition allows existing languages to develop and borrow features from new ones - developers have the opportunity to learn new things while avoiding burnout - there is a choice for beginners - there is a choice for specific tasks But why do most people dislike the C language so much? But it remains the fastest among high-level languages. Who benefits from C being suppressed and attempts being made to replace him? I think there is only one answer - companies. Not developers. Developers are already reproducing the opinion imposed on them by the market. Under the influence of hype and the opinions of others, they form the idea that C is a useless language. And most importantly, oh my god, he's unsafe. Memory usage. But you as a programmer are (and must be) responsible for the code you write, not a language. And the one way not to do bugs - not doing them. Personally, I also like the Nim language. Its performance is comparable to C, but its syntax and elegance are more modern. And in general, I’m not against new languages, it’s a matter of taste. But when you learn a language, write in it for a while, and then realize that you are burning out 10 times faster than before, you realize the cost of memory safety. This is that cost: ![](https://programming.dev/pictrs/image/09bab9f8-415a-47de-b951-2e5ec8a76df0.png)

24
33
c_lang
C Programming Language Scribbler9000 9 months ago 100%
My SDL Program isn't working

So I'm making this program in SDL to just render a single window and it's not working IDK why. It compiles well no errors or anything and then when I try to run it nothing happens at all. No text appears nor a window. I tried to run my own code but it didn't work so I went to try and run the code off the website and that didn't work either. CODE: /*This source code copyrighted by Lazy Foo' Productions 2004-2024 and may not be redistributed without written permission.*/ //Using SDL and standard IO #include <SDL.h> #include <stdio.h> #include <stdbool.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main( int argc, char* args[] ) { //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); } else { //Create window window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( window == NULL ) { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); } else { //Get window surface screenSurface = SDL_GetWindowSurface( window ); //Fill the surface white SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) ); //Update the surface SDL_UpdateWindowSurface( window ); //Hack to get window to stay up SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } } } } //Destroy window SDL_DestroyWindow( window ); //Quit SDL subsystems SDL_Quit(); return 0; }

3
1
c_lang
C Programming Language Debuholden 10 months ago 100%
What are some popular applications that use C?

Hey, so I've been searching the web for a while for some popular applications that use C. All that I can find so far is, Git, Vim, Linux, and Unix. I also know the Windows kernel uses it a little bit, but that's it. Does anyone know some popular apps that use C? Doesn't have to be programming related, just an actual app that's written in C? Sorry if this question sounds redundant or anything.

15
12
c_lang
C Programming Language Debuholden 10 months ago 100%
Is it possible to create a dynamic array without explicitly defining a variable to specify the length?

I'm trying to create a dynamic array which can be modified using the functions `Array_Push(array, val)` &amp; `Array_Del(array, index)`. Now the current way I have this I need a variable to keep track of the size of it. My implementation of this concept is to store the data/size in a struct like so:  `struct Array {`   `void **data;`   `int size;`  `}` However in order to read the actual array you have to type `array.data[i]` which I think is a little bit redundant. My solution to this was attempting to store the size of the array in a different index. I didn't want to store it inside `[0]` as that would create a lot of confusion, so I wanted to try storing it inside of `[-1]`. An obvious problem with this is that `[-1]` is outside the array. What I did instead was create an array via `void **array = malloc(sizeof(void*) * 2)` (the `* 2` is so when you push with `realloc()` it doesn't free empty memory,) then setting the size via `array[0] = (void *)0`. After that I increment the pointer to it via `array += 1`. However when I try to free it `free(array - 1)`, I end up freeing non `malloc()`ed. I think this is just an issue with my understanding of pointers, so I wanted to ask where my logic is going wrong, along with if anybody actually knows how to do what I'm trying to do (in the title).

13
2
c_lang
C Programming Language waspentalive 1 year ago 87%
#! a question about shebang.

Hi experienced people! I am working on an interpreter of sorts. I would like its scripts to be invokable from the command line - so It would honor the "#!" as a first line, basically by making any line starting with a "#" a comment. The issue is that I want to be able to read the source code more than once. The first pass will deduce the number of lines, the number of variables, the number of line labels, The beginning of the second pass will allocate arrays (malloc) to hold the program and its data, then re-read the source to store it internally and fill symbol tables and mark variables. once the source is read the 2nd time the program will begin to execute. If an interpreted program is mentioned on the command line it would only get one pass at the source, right? That source would come in on standard input, and once read is no longer available. Is there a way for my interpreter to get the file name instead of the body of the file? While writing the question I came up with an idea, but I hope there is a better one. I could as a first pass store each line of the program in a known temporary file, then for the second pass I could read that file. I don't like this but if there is no better way...

6
6
c_lang
C Programming Language rastignac 1 year ago 100%
Is it possible to use make to automatically link files well?

I'll explain myself. I'm doing a small project using a few dependencies, so I have to add them when I call gcc. Everything else is really easy. I just have c files and header files. I find it really cumbersome to have to tell make what headers go with what c files when they have the same name. I don't see why we don't have a build system where you simply have to give a project folder with the name of source file with the main() function, give the name of the output executable, the external dependecies to be called with gcc, and that's it. Everything else can be automatically detected and linked apropriately, even with multiple folders inside the project folder. Does something like that exist that's simple to use, or is this doable in make?

6
7
c_lang
C Programming Language 0x0 1 year ago 100%
Actually Portable Executable https://justine.lol/ape.html

Not new, but a rabbit hole nonetheless.

11
4
c_lang
C Programming Language rastignac 1 year ago 100%
How do dynamic libraries work with gcc? (example using Raylib)

Hello, I've installed the library called Raylib (https://www.raylib.com/). I followed the instructions to install the dynamic library. However, when compiling using gcc, including the raylib.h header dynamically in the source code produces an undefined reference error for every function I use from that library. That means that the library functions weren't attached to the executable at runtime, so gcc didn't find the right .so file. To actually produce an executable, I have to use a shell script, that for some reason needs raylib's source code path. But that goes against the point of installing a dynamic library, since the latter is used so that source code can be compiled without library functions, instead putting them somewhere else to be shared with other executables. Can someone explain to me how gcc finds the .so files and if anyone has used raylib dou you understand what the shell script does?

3
3
c_lang
C Programming Language rastignac 1 year ago 100%
pbm bitmap code note working as intended

Hi guys, I'm writing a program using the pbm "P1" format to create a random bitmap. The '1' char represents a black pixel and the '0' char represents a white pixel. All that has to be done to use this format is to basically "draw" the image in a file using this system. I just want to make a char matrix and randomly put black pixels until I hit a maximum percentage of black pixels which I don't want to exceed. Then I print that matrix to a file. What I don't understand is that even if I decrease the value of PERCENTAGE, recompile, and execute the program, there is no noticeable difference, in fact I suspect it's the same image, although I can't be sure. #include #include #define WIDTH 400 #define HEIGHT 250 #define TOTAL_PIXELS (WIDTH * HEIGHT) #define PERCENTAGE 0.01 #define BLACK_PIXEL '1' #define WHITE_PIXEL '0' ` int randomBrackets(int n){ return rand()/((RAND_MAX/n) + 1); } int main(){ char pbm_matrix[WIDTH][HEIGHT]; for(int i = 0; i &lt; HEIGHT; i++){ for(int j = 0; j &lt; WIDTH; j++){ pbm_matrix[i][j] = WHITE_PIXEL; } } int total_black_pixels = 0; while((double)(total_black_pixels/TOTAL_PIXELS) &lt; PERCENTAGE){ int x = randomBrackets(WIDTH); int y = randomBrackets(HEIGHT); pbm_matrix[x][y] = BLACK_PIXEL; total_black_pixels++; } FILE* img_ref = fopen("bitmap1.pbm", "w"); fprintf(img_ref, "P1 %d %d\n", WIDTH, HEIGHT); for(int i = 0; i &lt; HEIGHT; i++){ for(int j = 0; j &lt; WIDTH; j++){ fputc(pbm_matrix[i][j], img_ref); } fputc('\n', img_ref); } fclose(img_ref); return 0; }` This to open the image file netpbm is needed, and maybe libnetpbm (I don't know, everything is preinstalled on Linux Mint). I'm using the exercise in Rouben Rostamian's book as reference. EDIT: I'm sorry for the very poor formatting at the top of the code, I can't seem to get the macros to look good

5
1
c_lang
Suggestions for getting better with C

I just finished the C Piscine at a 42 school, so I have gotten a good grasp of the basics of C (about 300 hours worth). The school won't start until October, and I'd like to practice my C skills in the meantime so I can be better prepared when it does start. Any suggestions for curriculum / projects that don't start at the very beginning? I already have a good grasp of pointers, control structures, structs, string manipulation, etc.

12
4