-2

While watching a tutorial the speaker used

int deca['f' + '9' + 2 ] = {0};

I've never seen this on any other C++ tutorial and they didn't explain what it meant, and when I tried to implement it on my computer an error showed up.

As reference they were in a Linux environment.

Rodrigo Villalba Zayas
  • 5,326
  • 2
  • 23
  • 36

1 Answers1

2

It simply declares an integer array of N elements and initializes it to zero. What N evaluates to is determined by the 'f' + '9' + 2 expression. It evaluates to 161 if you are using ASCII code page or something else if you are using different code page. Every character literal has its corresponding integral value depending on the encoding used. In ASCII code page the character 'f' is represented by a number of 102 and the character '9' has a value of 57. The expression becomes 102 + 57 + 2 which equals 161. In other code pages those characters might have other values. Equivalent of:

int deca[161] = { 0 };  // If ASCII code page is used
Ron
  • 14,674
  • 4
  • 34
  • 47
  • 1
    That's only true if your system uses ASCII, which is not guaranteed [see related question](https://stackoverflow.com/questions/29381067/does-c-and-c-guarantee-the-ascii-of-a-f-and-a-f-characters) – Borgleader Oct 24 '17 at 12:56
  • 1
    @Borgleader Indeed. Updated the answer. – Ron Oct 24 '17 at 13:01
  • I think you mean _encoding_, not _code page_. Those two terms mean different things. E.g., a machine need not support _code pages_ to have an _encoding_. – H Walters Oct 24 '17 at 14:18
  • @HWalters I think you are right. I have updated the answer. – Ron Oct 24 '17 at 14:20