1

I expect it would break some other parse but this is not immediately springing to mind...

E.g. If I say this:

#include <stdio.h>
int main()
{
    int i = 10 000;
    printf("%d\n", i);
    return 0;
}

My compiler says this:

ctest.c:5: error: expected ‘,’ or ‘;’ before numeric constant

I'm thinking that it would be a feature, i.e. it's easier to 'eye parse' large integers with thousands separators. I'm using a recent GCC and assuming this is the case for other compilers/parsers/lexers; whichever it is that objects.

dpj
  • 933
  • 1
  • 7
  • 14

2 Answers2

6

The language doesn't allow this (an integer literal is one token, the intervening whitespace splits it into two tokens) but there's typically little to no expense incurred by expressing the initializer as an expression that is a calculation of literals:

int i = 10 * 1000; /* ten thousand */
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Does this compile to the same code? Or will the calculation of the multiplication be in the binary too? – Nobody moving away from SE Oct 08 '11 at 11:02
  • @Nodody: That's implementation dependent. On gcc the constant `10000` is used in the generated assembler in both cases, even at `-O0`. This implies that gcc doesn't even consider this an optimization. – CB Bailey Oct 08 '11 at 11:03
  • 2
    @CharlesBailey Your solution for `10 * 000` is good but what about `41196312`? I'm not a fan of `41 * 1000 * 1000 + 196 * 1000 + 312`. Would it have been considered adding needless complexity to the parser? Perhaps, also, sentiment against magic numbers in code? – dpj Oct 08 '11 at 11:22
  • 1
    @user710408: You're not going to get lost counting the digits in 41196312 because it's not a stream of identical zeros which was the use case that I was addressing. You could, I supposed, use token pasting if you wanted to break things up. e.g. `#define MY_CONST (41 ## 196 ## 312)` and `int i = MY_CONST;`. Incidentally, what is the origin of 41196312? I can't immediately see anything familiar about it. – CB Bailey Oct 08 '11 at 12:00
  • 1
    @CharlesBailey First, thanks for your responses Charles, much appreciated. `41 196 312` is the genomic coordinate of a well known gene on human chromosome 7. This seems to have degenerated into a debate about whether it's helpful to group the digits in large integers. I'm surprised since I would have thought that was a given, it's certainly common practice. Since the only answer that appears forthcoming is _because_ _the_ _standard_ _says_ _so_ I'll accept it! – dpj Oct 08 '11 at 13:41
2

Well it would conflict with the ability to overload the space operator that is proposed for c++.

(Yes I'm aware that's not a serious proposal).

EDIT: That was just a silly thing above, my real comment is the part below

For me the ability to put whitespace in numbers would both enhance and reduce readability. If you need to read the numbers themselves this would probably make it easier to do so. But I think it would also make the code slower to read because you'd have to mentally take one extra step parsing the code in your head rather than just looking to the end of the token. Assuming you're not that interested in the actual value of a number when reading it.

I don't believe there is any technical reason this couldn't be done, it's more that people see a number as a single indivisible token so it should be written like that. I have to say that I don't exactly know why but it would irritate me to see numbers broken up like that in code. I can't say there is a good reason for that, but it's still true :) It's probably just what I'm used to.

jcoder
  • 29,554
  • 19
  • 87
  • 130
  • 2
    That proposal was a C++ proposal; this question is about C. The potential conflict you suggest wouldn't be relevant. – CB Bailey Oct 08 '11 at 11:11
  • Ah ok. Yes. It wasn't a serious proposal anyway, it was an april fool joke or something. Plus it was just a silly comment on my main point anyway. – jcoder Oct 08 '11 at 11:14
  • If C implemented this as some magic token concatenation, C++ would have a problem with an operator overloading mechanism. In `10 012` the second literal would be octal requiring some very special magic to have the intended effect. – CB Bailey Oct 08 '11 at 11:17
  • But c++ space operator overloading wasn't/can't be serious can it?! So this wouldn't preclude having space separators in integers? – dpj Oct 08 '11 at 11:26
  • @JohnB If you are not interested in the value of the number then there's no advantage in breaking it up, clearly. If you are interested in the magnitude of the number, e.g 100s of millions or 10s of millions then it's very helpful I find. – dpj Oct 08 '11 at 11:38