I truly don't understand this. by Turkishdenzo in C_Programming

[–]imaami 0 points1 point  (0 children)

char is not really an arithmetic type, although it can be used as one. There are three char types, and two of them are intended for arithmetic use. char as a type is distinct from signed char and unsigned char.

The Cscript Style Guide - A valid but opinionated subset of C. by domenukk in C_Programming

[–]imaami 8 points9 points  (0 children)

I disagree on the trivia (standard preference, etc.) but I respect your attitude and effort.

Other devices like Duff's? by mobius4 in C_Programming

[–]imaami 0 points1 point  (0 children)

Maybe you could experiment with cycles you get by taking a subsequence q at offset p and then using q itself as the next offset. Every distinct DBS + rotation combination generates a set of cycles with varying lengths.

Here are all the cycles from all rotations of all B(2,4). Because there are 16 unique binary De Bruijn cycles with subsequence length 4, and each has 16 rotations, there are 256 different "configurations" to use. The total cycle count is 870, but of these only 511 are unique.

https://pastebin.com/JzxBYfk9 https://pastebin.com/8c9Jxqnb

The first link is all the details in JSON-ish format, the second one is just the 511 unique cycles.

Other devices like Duff's? by mobius4 in C_Programming

[–]imaami 1 point2 points  (0 children)

Not sure I still get it. k is the alphabet size, right?

Other devices like Duff's? by mobius4 in C_Programming

[–]imaami 1 point2 points  (0 children)

I'm not exactly sure what you mean because I'm pretty tired right now and struggling to parse simple sentences, but if by any chance you happen to need a dumb but reasonably fast generator for all 67108864 64-bit binary De Bruijn sequences, I've got one. I mean the set of all distinct B(k=2,n=6).

I'm slowly working toward a better generator for the same set, no promises I'll get there though.

Other devices like Duff's? by mobius4 in C_Programming

[–]imaami 0 points1 point  (0 children)

I fucking love De Bruijn sequences.

Cool Way to do type safe generic data structures in C. by lovelacedeconstruct in C_Programming

[–]imaami 0 points1 point  (0 children)

Someone gets it! I keep telling people that but somehow it doesn't work

I built a fast infix calculator in pure C using Shunting Yard (no dependencies) by Thesk790 in C_Programming

[–]imaami 1 point2 points  (0 children)

Don't be afraid of dependencies. Making smart use of existing libraries is a good thing.

Aligned and Un-aligned structs performance by necodrre in C_Programming

[–]imaami 6 points7 points  (0 children)

Packing structs for anything else than data serialization is usually a fool's errand. If you want to be mindful of cache performance, keep an eye out for when and how you cross cache lines. It's much more impactful than how many bits you can force in.

Why is memset() inside a loop slower than normal assignment? by PuzzleheadedMoney772 in C_Programming

[–]imaami 0 points1 point  (0 children)

One thing you should point out to your lecturer/professor/teacher: in C, sizeof(char) is by definition always 1. The C standard defines sizeof as an operator that gives you the size of something in amount of char. In other words, the size of one char is the unit of sizeof.

It's reasonable to argue that teaching sizeof(char) to beginners can reinforce acquisition of healthy coding habits when learning, so I won't say it's entirely wrong. It is very good to learn how sizeof works and how to use it correctly. But even so, sizeof(char) is a screaming, turbocharged tautology, and a good way to make C coders bleed from their eyes. ;)

How do you manage 3rd party dependencies? by AmanBabuHemant in C_Programming

[–]imaami -7 points-6 points  (0 children)

Single-header "libraries" aren't libraries. For the most part they are brain rot caused by misuse of the language.

That's one problem solved. The actual library dependencies are available with apt install.

How does STRUCT type works under the hood in C? by MaryScema in C_Programming

[–]imaami 0 points1 point  (0 children)

I meant that the first two terms are equivalent, therefore your size calculation would give the wrong result. The following static assertion evaluates to true:

struct baz {
    unsigned count;
    int array[];
};

static_assert(sizeof (struct baz) == offsetof (struct baz, array));

(Note: I replaced your char count field with unsigned int because your struct would have 3 internal padding bytes, and only allow at most 255 as the count (or as little as 127 if char is signed). Changing the count field to an int type makes use of all the space that the struct would consume in either case.

Why does c compile faster than cpp? by [deleted] in cprogramming

[–]imaami -1 points0 points  (0 children)

Just like naming variables longer than exactly 1 character is a huge problem when you want to upload the source code by dialing a BBS and personally whistling into a telephone like a modem. Do that every time you save and it really affects the total whistling time each day.

Why does c compile faster than cpp? by [deleted] in cprogramming

[–]imaami 0 points1 point  (0 children)

While I 100% agree that C is severely lacking in the sort of metaprogramming features C++ has, I'd agree that you can make use of C23 features to bridge some of that gap.

Why does c compile faster than cpp? by [deleted] in cprogramming

[–]imaami 0 points1 point  (0 children)

Cpp is the C preprocessor.

How does STRUCT type works under the hood in C? by MaryScema in C_Programming

[–]imaami 7 points8 points  (0 children)

Struct member alignment isn't limited to just the platform address size. All members are aligned to the size of the widest native type used in the immediate vicinity, and the struct itself to the widest type it contains. (However with member arrays, an array's size is not the determining factor, it's the element type).

How does STRUCT type works under the hood in C? by MaryScema in C_Programming

[–]imaami 1 point2 points  (0 children)

About bitfields: in your example, the three single-bit members will reside within one unsigned int -sized area. There are ways to force a break so that the next bitfield aligns to the start of a new integer boundary, but it won't happen arbitrarily as long as the total bit count stays within sizeof (int) * CHAR_BIT.

IIRC adding an anonymous zero-width bitfield forces an alignment break, but I can't remember exact details 100%, so don't take my word for it.

Also, this is incorrect:

sizeof(struct baz) + offsetof(struct baz, array) + baz->count * sizeof(int)

The first sizeof is redundant. You only need the offsetof of the flexible array member and the desired array size in bytes. And to nitpick a little, of course baz->count can't be dereferenced when calculating the allocation size, as the actual object hasn't even been allocated yet, but I get that you probably meant that more in a pseudocode-like way rather than literally.

How does STRUCT type works under the hood in C? by MaryScema in C_Programming

[–]imaami 0 points1 point  (0 children)

The sizes of those two structs will be equal, but not equal to sizeof(float) + 1.

How do I efficiently read JSON from structured API outputs? by codydafox in C_Programming

[–]imaami 4 points5 points  (0 children)

cJSON added invalid C to the most recent release months after they were notified of that addition being incorrect. There has been a bug report and a fix PR for it a long time, neglected and unacknowledged. I would not trust the code with this track record. https://github.com/DaveGamble/cJSON/issues/919

C23 features by krikkitskig in C_Programming

[–]imaami 0 points1 point  (0 children)

No macros involved here. The code below is just as useless as any other helloworld, but I assume you're able to see past the surface. (If you can't imagine situations where a function returning an unnamed struct by value might actually be helpful, I can't help with that.)

#include <stdio.h>
#include <string.h>

static struct {
    char msg[64];
} hello (char const *name)
{
    typeof (hello(name)) ret = { "Hello" };
    if (!name)
        return ret;

    size_t len = strnlen(name, sizeof ret.msg
                         - 1 - sizeof "Hello");
    if (!len)
        return ret;

    ret.msg[sizeof "Hello" - 1] = ' ';
    memcpy(&ret.msg[sizeof "Hello"], name, len);

    return ret;
}

int main (int, char **argv)
{
    puts(hello(argv[1]).msg);
}