The C Programming Language

"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
How is MS-DOS able to store seconds in just 5 bit-field?

Second representation requires at least 6 bits to represent numbers between 0 to 59. But 5 bits are not just enough - 2^5^ = 32, which can only represent from 0 up to 31 seconds. According to K.N. King: >You may be wondering how it 's possible to store the seconds - a number between 0 and 59 in a field with only 5 bits. Well. DOS cheats: it divides the number of seconds by 2, so the seconds member is actually between 0 and 29. That really makes no sense?

5
15
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Segmentation fault for a valid program?

In the K.N.King book, there's an example: ::: spoiler **viewmemory.c** ```c /* Allows the user to view regions of computer memory */ #include <stdio.h> #include <ctype.h> typedef unsigned char BYTE; int main(void) { unsigned int addr; int i, n; BYTE *ptr; printf("Address of main function: %x\n", (unsigned int) main); printf("Address of addr variable: %x\n", (unsigned int) &addr); printf("\nEnter a (hex) address: "); scanf("%x", &addr); printf("Enter number of bytes to view: "); scanf("%d", &n); printf("\n"); printf(" Address Bytes Characters\n"); printf(" ------- ------------------------------- ----------\n"); ptr = (BYTE *) addr; for (; n > 0; n -= 10) { printf("%8X ", (unsigned int) ptr); for (i = 0; i < 10 && i < n; i++) printf("%.2X ", *(ptr + i)); for (; i < 10; i++) printf(" "); printf(" "); for (i = 0; i < 10 && i < n; i++) { BYTE ch = *(ptr + i); if (!isprint(ch)) ch = '.'; printf("%c", ch); } printf("\n"); ptr += 10; } return 0; } ``` ::: For some reason, when I try to enter addr variable address as the parameter, it has a segmentation fault error. However, in the book's example and the screenshot from this [site in Hangul](https://ziegler.tistory.com/84), there's no such error? When I try using `gdb` to check the issue, here's what I get: :::spoiler gdb ```console $ gdb ./a.out --silent Reading symbols from ./a.out... (gdb) run Starting program: /home/<username>/Desktop/c-programming-a-modern-approach/low-level-programming/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/gnu/store/zvlp3n8iwa1svxmwv4q22pv1pb1c9pjq-glibc-2.39/lib/libthread_db.so.1". Address of main function: 401166 Address of addr variable: ffffd678 Enter a (hex) address: ffffd678 Enter number of bytes to view: 64 Address Bytes Characters ------- ------------------------------- ---------- Program received signal SIGSEGV, Segmentation fault. 0x000000000040123a in main () at viewmemory.c:31 warning: Source file is more recent than executable. 31 printf ("%.2X ", *(ptr + i)); (gdb) ``` ::: What is going on? By the way, I am using Guix, if that matters in any way. Here's the output for `ldd`: :::spoiler ldd ```console $ ldd ./a.out linux-vdso.so.1 (0x00007ffecdda9000) libgcc_s.so.1 => /gnu/store/w0i4fd8ivrpwz91a0wjwz5l0b2ralj16-gcc-11.4.0-lib/lib/libgcc_s.so.1 (0x00007fcd2627a000) libc.so.6 => /gnu/store/zvlp3n8iwa1svxmwv4q22pv1pb1c9pjq-glibc-2.39/lib/libc.so.6 (0x00007fcd2609c000) ``` :::

2
1
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
What exactly is linkage in C?

It looks very similar to scope, so I really don't understand the difference. What makes storage classes different from each other? How is `auto` not the same as `static`, `extern` or `register`?

3
2
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
`#if` vs `#if defined` vs `#ifdef` directives - how are they different from each other?
11
3
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Are there better alternatives to null-terminated strings?
2
4
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
fatal error: dbus/dbus-arch-deps.h: No such file or directory

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 ```

5
1
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
open source structured thread safe logging library for C

Is there a library for C, providing thread safe (high performance), and structured logging? An example for rust is the Tracing crate for rust (from Tokio). It should support several outputs as well.

5
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
GNU C Library 2.38
https://lists.gnu.org/archive/html/info-gnu/2023-07/msg00010.html

cross-posted from: https://lemmy.ml/post/2650558 > **Highlights**: > > - More work on C2X features. > > - The strlcpy and strlcat functions have been added. They are derived from OpenBSD, and are expected to be added to a future POSIX version. > > - Support for x86_64 running on Hurd has been added. > > - CVE-2023-25139: When the printf family of functions is called with a > format specifier that uses an (enable grouping) and a > minimum width specifier, the resulting output could be larger than > reasonably expected by a caller that computed a tight bound on the > buffer size. The resulting larger than expected output could result > in a buffer overflow in the printf family of functions.

5
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Enforce same size for arrays at compile-time?

Let's say I have two arrays that have related data: ``` const char *backend_short[] = { "oal", "pa", "sdl_m" }; const char *backend_long[] = { "openal", "portaudio", "sdl_mixer" }; ``` Does C support a way to "assert" that these two arrays have the same size? And failing compilation if they are different? I want a safeguard in case I'm drunk one day and forget to keep these synchronized. Thanks in advance. EDIT: I found a solution. Here are some enlightening resources on the matter: * https://www.pixelbeat.org/programming/gcc/static_assert.html * https://www.embedded.com/catching-errors-early-with-compile-time-assertions/

3
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Readline from stdin

If one has POSIX extensions available, then it seems that defining _POSIX_C_SOURCE and just using getline or detdelim is the way to go. However, for source that is just C, here is a solution I've found from various sources, mainly [here](https://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length). I've also tried to make it more safe. ``` // Read line from stdin #include <stdio.h> #include <stdlib.h> #define CHUNK_SIZE 16 // Allocate memory in chunks of this size // Safe self-realloc void *xrealloc(void *ptr, size_t size) { void *tmp = realloc(ptr, size); if (!tmp) free(ptr); return tmp; } // Dynamically allocate memory for char pointer from stdin char *readline(void) { size_t len = 0, chunk = CHUNK_SIZE; char *str = (char *)malloc(CHUNK_SIZE); if (!str) return NULL; int ch; while((ch = getchar()) != EOF && ch != '\n' && ch != '\r') { str[len++] = ch; if (len == chunk) { str = (char *)xrealloc(str, chunk+=CHUNK_SIZE); if (!str) return NULL; } } str[len++] = '\0'; // Ensure str is null-terminated return (char *)xrealloc(str, len); } int main(void) { setbuf(stdout, NULL); // Ensure environment doesn't buffer stdout printf("Enter name: "); char *userName = readline(); if (!userName) return 1; printf("Hello, %s!\n", userName); free(userName); return 0; } ``` The idea is that we allocate in chunks of 16, or whichever size. xrealloc is handy when reallocating a block of memory to itself. ``` int *ptr = malloc(sizeof(int) * 4); ptr = (int *)realloc(ptr, sizeof(int) * 8); ``` If realloc fails, ptr gets assigned `NULL`, and we lose the address that we need to free, causing a memory leak. So, xrealloc allows for safe self re-allocation.

5
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
My journey through the first C implementation of (m)alloc
https://ljrk.codeberg.page/unixv6-alloc.html

Did some digging in The Unix Heritage Society's copy of Research UNIX v6 and wrote what I learned from reading decade-old C. Quite much, it turned out! Markdown source available at https://ljrk.codeberg.page/unixv6-alloc.md

6
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Linus Torvalds on C++
http://harmful.cat-v.org/software/c++/linus
4
4
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
The Spirit of C
beza1e1.tuxen.de
5
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
dwl is a dwm-like Wayland compositor based on wlroots
github.com

I am *not* the author of dwl. Since dwl is based on wlroots (just like the popular Sway) it already supports making screenshots using [grim][2] and screencasts using [wf-recorder][3]. You can try out dwl from within your current WM. The default modkey is `Alt`. If you want to use the Super key, change `#define MODKEY WLR_MODIFIER_ALT` in `config.h` to `#define MODKEY WLR_MODIFIER_LOGO` and recompile. The default terminal emulator is kitty but you can change `termcmd` to alacritty if you want. Currently, only native Wayland applications run on it. You can enable experimental Wayland support for Firefox with `MOZ_ENABLE_WAYLAND=1`, see [Running programs natively under Wayland in Sway Wiki][1]. Do not hover over the edges of windows – this will crash dwl. [1]: https://github.com/swaywm/sway/wiki/Running-programs-natively-under-wayland [2]: https://github.com/emersion/grim [3]: https://github.com/ammen99/wf-recorder

1
1
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
In browser JS to C compiler
https://timr.co/in-browser-js-to-c-compiler
1
0
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Rust vs C gcc - Which programs are fastest?
https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/rust.html
4
1
"Initials" by "Florian Körner", licensed under "CC0 1.0". / Remix of the original. - Created with dicebear.comInitialsFlorian Körnerhttps://github.com/dicebear/dicebearC_
Looking for a programming buddy to study C together

Hello fellow lemmys (or however do we call us?), as the title suggests, I am looking for someone who wants to learn the C programming language. I have a decent knowledge of computer science and of a couple of programming languages (C#, Java, Python). Currently, I am studying computer science in central/western Europe. In my opinion, studying together keeps each other motivated and is more efficient. In long terms, I hope, that we can contribute to open-source projects together and improve our code quality by reviewing each others code. If any of you has some interest in being my programming buddy, please message me or reply to this post. Preferably from a similar time zone (mine is UTC+01:00). Hoping for some replies Regards

6
1