65

I have the following source:

int main() { 000J; }

With gcc 4.8.4 it compiles without errors. I know there are suffixes like L or U, but I didn't find anything about J.

So what does it do?

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
happyMOOyear
  • 1,255
  • 1
  • 11
  • 14
  • 7
    Make sure you compile with `-std=c++11 -pedantic` to avoid compiler extensions. By default it will use `-std=gnu++11` (I think) which enables non-standard extensions. – Simple Nov 05 '15 at 16:15
  • You're right. With the `-pedantic` flag I get the same warning as zenith which points to the answer. – happyMOOyear Nov 05 '15 at 16:19
  • 5
    The C++ tag is misplaced here TBH. C++ only has [suffix `i`](http://en.cppreference.com/w/cpp/numeric/complex/operator%22%22i) not `J`. It is another type, `std::complex` instead of `_Complex`. And it's Standard C++, not an extension. – MSalters Nov 05 '15 at 17:33
  • @MSalters gcc has `J` as an extension for C++ as well. – Emil Laine Nov 05 '15 at 19:41
  • @zenith: True, the g++ tag would make sense. – MSalters Nov 06 '15 at 10:21

2 Answers2

74

I get a warning:

Imaginary constants are a GNU extension

The J suffix is a GNU extension, which causes the literal to be of a _Complex type.

More info here: https://gcc.gnu.org/onlinedocs/gcc/Complex.html

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Hm, I didn't get that warning. Which version of gcc are you using and did you use any compile flags? – happyMOOyear Nov 05 '15 at 16:16
  • 1
    @happyMOOyear Try `-pedantic`. – Spikatrix Nov 05 '15 at 16:17
  • 2
    I'm using Apple LLVM 7.0 with `-pedantic`. – Emil Laine Nov 05 '15 at 16:19
  • 6
    @Yakk as a mathematician, I take grave exception to this. WTH is a "Jamaginary Number"??? – geometrian Nov 06 '15 at 00:45
  • 7
    @imallett that's very similar to "WTH are Zintegers and Qationals???" (of course I know that's due to non-English origin, but still). – Ruslan Nov 06 '15 at 09:57
  • 1
    @Ruslan Q comes from "quotients", which makes sense. Z comes from German. Fortunately for my argument, "imaginary" in German is "imaginär", which still starts with `i` :) – geometrian Nov 06 '15 at 16:40
  • @imallett j is standard notation for the y component of a vector, in this case it is the unit vector on the imaginary axis. i is used as the unit vector on real axis. So 1 + 1i (where i=sqrt 1) can be written as 1i + 1j (where i=unit). That's where it comes from. – corazza Nov 13 '15 at 17:49
  • @jcora That is literally the first credible argument I've heard for using `j`. Still, the standard basis is defined in R^n, as `i:=<1,0,0,0,0...>, j:=<0,1,0,0,0,...>, k:=<0,0,1,0,0,...>`. Abusing the notation for complex numbers is doing something _literally orthogonal_ to this correct meaning . . . – geometrian Nov 13 '15 at 20:43
  • @imallett as someone from electronics background I should note that `j` is mainly used in electronics and electrical engineering to differentiate from `i` which is electric current – phuclv Oct 17 '16 at 04:07
  • @jcora I don't think it's correct ["In electrical engineering and related fields, the imaginary unit is normally denoted by j to avoid confusion with electric current as a function of time, traditionally denoted by i(t) or just i. The Python programming language also uses j to mark the imaginary part of a complex number."](https://en.wikipedia.org/wiki/Imaginary_unit#Alternative_notations). [Why are complex numbers in Python denoted with 'j' instead of 'i'?](http://stackoverflow.com/q/24812444/995714) – phuclv Oct 17 '16 at 04:08
  • @LưuVĩnhPhúc That does seem like the more probable reason, thanks! – corazza Oct 26 '16 at 08:24
15

As zenith mentioned, this is a GNU extension for writing imaginary literals. I really want to comment on the rationale of using j for this purpose as imallett is wondering but I don't have enough reputation to comment on zenith's answer. I'll leave this as an answer anyway as it might be helpful to others.

As this link explains, both i and j can be used to write imaginary literals using this GNU extension. The reason why i is used for this is obvious, but the reason why j is used as well is that j is commonly used to denote the imaginary unit in electrical engineering and control systems engineering to prevent confusion as i is already used to denote electrical current in those contexts.

Jordan Melo
  • 1,193
  • 7
  • 26
  • 1
    This answer is even better, because the number becomes imaginary, not complex, the former being a subset of the latter. – Dirk Nov 11 '15 at 14:07
  • `i` and `j` are also by convention often defined slightly differently such that `exp(-ix) = exp(jx)`. – Forss Nov 29 '15 at 16:36
  • @Forss Interesting - any reference for that claim? – chux - Reinstate Monica Nov 30 '15 at 23:56
  • @chux Found [this pdf](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-007-electromagnetic-energy-from-motors-to-lasers-spring-2011/readings/MIT6_007S11_sign.pdf) that explains the difference in convention. – Forss Dec 01 '15 at 18:06