4 ms·
What does 0100 actually do? Is it not supposed to be 64_{10}?
by Something1234 8y ago
What does 0100 actually do? Is it not supposed to be 64_{10}?
- steveklabnik 8y agoIt does make a value of 64 decimal. This is really surprising to a lot of people. It's really nice when you're dealing with unix permissions, and a potential footgun elsewhere.
- dnautics 8y agoA potentially better syntax would be 0o100, e.g.
- pjc50 8y ago> 0o100 This is some IOCCC level stuff, not distinguishable in all fonts. I'd rather borrow a couple of ideas from Verilog, including the cosmetic underscore and the ability to represent binary, e.g. 8'b0101_1100
- jandrese 8y agoYou could even support binary that way: 0b10101101 If you standardize this the leading zero becomes somewhat unnecessary. But of course it's probably too late for C. You would have to depreciate the old notation for decades before it could be removed.
- kazinator 8y agoDeprecating is as simple as emitting a diagnostic whenever it occurs, rate-limited to once per translation unit, say.
- jandrese 8y agoYes, but it also means fixing all of that old code before you can turn the depreciation into full on removal. It would be interesting to note that such warnings would undoubtedly point out a few errors in existing code where people 0 padded a number they expected to be decimal. Many would be harmless, 00001 for example, but a few could have been causing unintended side effects (incorrect initialization values for example).
- steveklabnik 8y ago(As I mentioned above, Rust does exactly this; 0o for octal, 0b for binary.)
- jandrese 8y ago0 prefix octal notation is a mistake. It bites people in the strangest places, like when they decide to make the IP addresses line up in a file by changing: 192.168.1.10 192.168.130.22 to: 192.168.001.010 192.168.130.022 What they thought would be a cosmetic change breaks their application. I've seen this happen several times. The new replacement for inet_addr (inet_pton) removes this braindamage, but it is not available on Windows.
- Something1234 8y agoI always thought I had to quote my ip addresses whenever I do socket programming? Is that not the case? Or does one the parsing routines in C do something with the 0? I could understand it if I was using commas instead of dots, and passing the parts to a function, but I'm not, I'm quoting them as a string.
- jandrese 8y agoYou pass it in as a string, and the standard library does strtol() on each component. The problem is that strtol() is "smart" and tries to autodetect the base of the input. So you could specify your IP address as 0xc0.0xa8.0xa.0x1 if you wanted, but who the hell does that?