C Programming Language
- Why does TCC fail with GNU's regex.h?
TCC fails with GNU's regex.h:
```fish echo ' #include <regex.h>
int main() { return 0; }' | tcc - -run In file included from -:2: /usr/include/regex.h:682: error: '__nmatch' undeclared ```
Would building it from source fix the issue? I know from my experience that Pop_OS! apt repositories are not very up-to-date with smaller apps. For example, NekoVM is from 2017, the latest one is from 22 I believe.
- A History of C Compilers - Part 1: Performance, Portability and Freedomthechipletter.substack.com Freedom and Portability
Let's talk compilers with part one of a whistle stop tour of their history
- One year of C (2018)floooh.github.io One year of C
It’s now nearly a year that I started writing non-trivial amounts of C codeagain (the first sokol_gfx.h commit was on the 14-Jul-2017), so I guess it’stime f...
- Blackjack game as a learning experience - Request for suggestions, ways to make it better, advice etc...github.com GitHub - promitheas17j/blackjack: CLI Blackjack (21) written in C as practice
CLI Blackjack (21) written in C as practice. Contribute to promitheas17j/blackjack development by creating an account on GitHub.
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 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!
- Improvements to static analysis in the GCC 14 compiler | Red Hat Developerdevelopers.redhat.com Improvements to static analysis in the GCC 14 compiler | Red Hat Developer
Learn about static analysis improvements coming in GCC 14 with -fanalyzer, which helps identify issues in C code at compile-time, rather than at runtime.
- How to write a good C main functionopensource.com How to write a good C main function
Learn how to structure a C file and write a C main function that handles command line arguments like a champ.
- C can be memory-safe (2023)blog.erratasec.com C can be memory-safe
The idea of memory-safe languages is in the news lately. C/C++ is famous for being the world's system language (that runs most things) but ...
- Modern C, Third Edition (covers the C23 standard)
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.
- What do you think about brains unsafety?
Maybe you have to hear about this.
What do you think?
I think:
For infrastructure technology, C will be hard to displace. © Dennis Ritchie
- 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;
- White House urges developers to dump C and C++www.infoworld.com White House urges developers to dump C and C++
Biden administration calls for developers to embrace memory-safe programing languages and move away from those that cause buffer overflows and other memory access vulnerabilities.
- Matrix space for the C programming language
I have created matrix space for the C programming language.
Rooms:
- #c-lang:bsd.cafe ...programming in the C language in general
- #c-game-dev:bsd.cafe ...game development in the C programming language, game engines, physics, ecs
- #c-embedded-dev:bsd.cafe ...embedded systems development in the C programming language
- #c-sys-dev:bsd.cafe ...system programming in the C language, os, drivers
- #c-web-dev:bsd.cafe ...web development in the C programming language, client, server, wasm
- #c-graphics-dev:bsd.cafe ...graphics development in the C programming language, 2d, 3d, GUI
- #c-lang-dev:bsd.cafe ...programming languages development in the C language
- #c-ai-dev:bsd.cafe ...ai and data science using the C programming language, robotics
- #c-compiler:bsd.cafe ...anything related to the C programming language compiler, tools
- #c-learning:bsd.cafe ...learning the C programming language together, sharing resources, asking for help
- #c-showcase:bsd.cafe ...sharing own projects and development process in the C programming language
- #c-evangelism:bsd.cafe ...the C programming language will never die, philosophy, refutations
- #c-cafe:bsd.cafe ...coffee breaks, off-topic, miscellaneous from the C language programmers
- An implementation of Zephyr ASDL languagegithub.com GitHub - Chubek/ZephyrASDL: An implementation of Zephyr ASDL in C, targeting C
An implementation of Zephyr ASDL in C, targeting C - Chubek/ZephyrASDL
Keep in mind that it is still buggy, i need to take a break from it and finish it up later. ASDL is a domain-specific language used to make Abstract Syntax Trees.
Thank.
- AllocPP.pl -> Don't let this script go!gist.github.com AllocPP.pl -> A Perl Script that Preprocesses C and Creates Allocation Heaps and their Functions
AllocPP.pl -> A Perl Script that Preprocesses C and Creates Allocation Heaps and their Functions - AllocPP.pl
AllocPP.pl is a script I authored that creates a dynamic heap (as in datatype heap, not the heap segment of the process) in your program, and these heaps are allocatable, and reallocatable, and deallocatable. It does so by preprocessing your program. It's in no way a substitution for memory management and garbage collection, but you can use it for one-off programs and tests. It has two directives, '#alloc' and '#hashfunc'. The latter is not important, just name your hash function anything. But the former is important. There can be as many heaps in your programs as possible.
There's an example, and full explanation in the Gist.
Thanks.
- A Markdown Pager in C and Lexgithub.com GitHub - Chubek/Mukette: A TUI Pager for Markdown, a tool similar to man(1) or most(1), but renders Markdown in-terminal
A TUI Pager for Markdown, a tool similar to man(1) or most(1), but renders Markdown in-terminal - GitHub - Chubek/Mukette: A TUI Pager for Markdown, a tool similar to man(1) or most(1), but renders...
Hey. I have been posting this everywhere but few people seem to get what it is. Figured this will be the best place. Thanks.
- 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:
- C23: a slightly better Clemire.me C23: a slightly better C
One of the established and most popular programming languages is the C programming language. It is relatively easy to learn, and highly practical. Maybe surprisingly, the C programming language keeps evolving, slowly and carefully. If you have GCC 13 or LLVM (Clang) 16, you already have a compile
- 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; }
- The new oracles of GCC | Red Hat Developerdevelopers.redhat.com The new oracles of GCC | Red Hat Developer
This article summarizes the new improvements added to GCC optimizers in the form of an oracle.
- 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.
- 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)
&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 typearray.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 viavoid **array = malloc(sizeof(void*) * 2)
(the* 2
is so when you push withrealloc()
it doesn't free empty memory,) then setting the size viaarray[0] = (void *)0
. After that I increment the pointer to it viaarray += 1
. However when I try to free itfree(array - 1)
, I end up freeing nonmalloc()
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). - #! 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...
- 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?
- Actually Portable Executable
Not new, but a rabbit hole nonetheless.
- 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?
- A Modern C Development Environmentinterrupt.memfault.com A Modern C Development Environment
Spinning up a C development environment using CMake, Docker, Unity, and GitHub Actions.
- 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 < HEIGHT; i++){ for(int j = 0; j < WIDTH; j++){ pbm_matrix[i][j] = WHITE_PIXEL; } } int total_black_pixels = 0; while((double)(total_black_pixels/TOTAL_PIXELS) < 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 < HEIGHT; i++){ for(int j = 0; j < 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
- 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.
- What are approaches to write unit tests on code whose function is dependent on the underlying system or configuration?
Unit tests are meant to verify the functionality of isolated units of code. When dealing with code whose output depends on the system or system configuration, what are approaches to write effective unit tests? I feel this problem plagues lower level systems languages more so I am asking it here.
I solve this by writing "unit tests" that I then manually compare to the output of my terminal's utilities. It is the quickest way to verify units work as expected but it is obviously not automated.
Making a container or a VM to run integration tests seems like the next easiest way, not sure if there are other cost effective ways.
Scenario
Say I have a function called
get_ip_by_ifname(const char *if_name, struct in_addr *ipaddr)
Inputs:
- string of interface name
- pointer to variable where the returned IP address will be
Returns:
- -1 if interface does not exist,
- 0 if interface exists but has no IPv4 IP
- 1+ if interface exists and has at least 1 ip addr (some interfaces have multiple addresses, only 1st is written to ipaddr buffer)
Test Cases and their dependencies
- Interface doesn't exist
- easy to test, use uncommon interface name
- Interface exists has no ipv4 ip address
- requires the underlying system to have a unique interface name which I need to hard code and compare to in my unit test
- interface exists, has 1 ipv4 ip address
- requires underlying system to have the uniquely named interface with exactly 1 uniquely defined ip address. Both of which I need to hard code into my test
- interface exists, has 1+ ipv4 ip addresses
- similar to item 3.
The way I might test something like this works is write a test that logs each case's output to the terminal than run
ip -c a
in another terminal and compare the info in the 2 outputs. I verify it works as expected manually with very minimal setup (just assigned multiple IP addresses to one of my interfaces).I would like to test this in an automated fashion. Is there any way that wont be a time sink?
- A computational model of C. elegans wormgithub.com GitHub - openworm/OpenWorm: Repository for the main Dockerfile with the Openworm software stack and project-wide issues
Repository for the main Dockerfile with the Openworm software stack and project-wide issues - GitHub - openworm/OpenWorm: Repository for the main Dockerfile with the Openworm software stack and pro...
- Demystifying bitwise operations, a gentle C tutorialwww.andreinc.net Demystifying bitwise operations, a gentle C tutorial
A beginner-friendly tutorial on bitwise operations in C
Saw this on reddit, good quality tutorial.
- Problems of C, and how Zig addresses themavestura.dev Problems of C, and how Zig addresses them
What are the problems of C, and how Zig addresses them?