logoalt Hacker News

1718627440last Monday at 9:12 AM1 replyview on HN

> The C folks should just make it illegal.

I often have code, which looks like this:

    for (ptr = start; random_condition (*ptr); ptr = ptr->next);
    for (ptr = ptr->next; other_condition (*ptr); ptr = ptr->prev);

    ...  [do action]

    for (ptr = end; to_be_deleted (*ptr) && (delete (ptr), TRUE); ptr = ptr->prev);
I wouldn't be happy about your policy.

> I've also fixed printf in D so that [...] gives an error message

Just last week I had the case that the C compiler complained, I should use %lld for long long, but the printf implementation shipped with the compiler doesn't support that. Thus, using %ld, even if undefined behaviour was the correct action. I wouldn't like my language making up more work for me for no reason.


Replies

WalterBrightlast Monday at 5:51 PM

Rewrite as:

    for (ptr = start; random_condition (*ptr); ptr = ptr->next) { }
Then anyone reading your code will know the empty loop was intentional. BTW, many C compilers warn about the ; on a for loop.

Have you ever discovered this bug:

    if (condition);
        doThis();
It's a hard one to see in a code review. Yes, that's also disallowed in D.

> I should use %lld for long long, but the printf implementation shipped with the compiler doesn't support that.

Weird that your compiler would support long long, but with a broken Standard library. I don't see that as a feature. You can always cast the argument to long, and at least you won't get undefined behavior.

show 1 reply