this post was submitted on 11 Feb 2025
350 points (95.6% liked)

Programmer Humor

32710 readers
362 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 19 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 6 days ago (1 children)

counterpoint: all code is unsafe. retvrn to abacuses

[–] Ziglin 3 points 6 days ago

But I was hit over the head with one, that wasn't safe either!

[–] [email protected] 69 points 1 week ago

Strictly speaking, it should be

Unsafe block syntax in C++

{  ...}
[–] [email protected] 39 points 1 week ago* (last edited 1 week ago) (1 children)

That is why I use just int main(){...} without arguments instead.

[–] [email protected] 28 points 1 week ago* (last edited 1 week ago) (3 children)

Any void main(){...} enjoyers?

[–] [email protected] 7 points 1 week ago (2 children)

besides not requiring a return value, what difference does it make?

[–] [email protected] 9 points 1 week ago (1 children)

@stebo02 @Bogus5553 Neither of them require a return value, but void main isn't legal C++.

[–] [email protected] 2 points 1 week ago (1 children)

yeah I thought so, does it work in C?

[–] [email protected] 4 points 1 week ago* (last edited 1 week ago)

void main(){...} is not in the standard, but works on both MSVC and GCC (with warnings). I think it works on both C/C++, but you really shouldn’t use it in production. Just use int main(void){...} , without any return value, which is permitted in the standard, and will return success iirc.

[–] Buddahriffic 2 points 1 week ago

It will also give an error if you try to add a return value anyways.

[–] [email protected] 2 points 1 week ago

while (true) {...}

[–] [email protected] 1 points 1 week ago

int main(void) { . . . } for me!

[–] HStone32 19 points 1 week ago

Safe code is a skill, not a feature.

[–] affiliate 6 points 1 week ago (1 children)

i will never forgive C for making the type syntax be

char* args[]

instead of the much more reasonable

&[char] args 

it also bothers me that char* args[] and char c are “the same type” in the sense that the compiler lets you write

char c, *args[5];

with no problems. i think the C languages would be way easier to learn if they had better type syntax. don’t even get me started on C++ adding support for

auto fn_name() -> ReturnType { … }
[–] [email protected] 7 points 1 week ago (2 children)

@affiliate Hey, you didn't even mention that char *args[] actually means char **args in a parameter list.

[–] affiliate 5 points 1 week ago (1 children)

god, what a beautiful language. it brings a tear to my eye

[–] [email protected] 3 points 1 week ago

I personally think that C++ can be beautiful. For example: std::filesystem::path overrides the / operator, for specifying parent paths. It’s the same as Kotlin’s OKIO and Pythons standard pathlib.

[–] [email protected] 2 points 1 week ago (1 children)

It could, but not necessarily.

char **args can just mean you have a pointer which points to an address, and at that address, you can get a second address. Follow the second address, there is a char saved there.

On the other hand, char *args[] means " follow this address to find a list of characters".

[–] [email protected] 3 points 1 week ago

@racketlauncher831 As far as the C compiler is concerned, there is literally no difference between those two notations. If you declare a function parameter as an array (of T), the C compiler automatically strips the size information (if any) and changes the type to pointer (to T).

(And if we're talking humans, then char *args[] does not mean "follow this address to find a list of characters" because that's the syntax for "array of pointers", not "pointer to array".)