-3

I wrote the code below:

#define int64_t long long int
typedef long long int64_t; //this line is line 2
void main()
{
int64_t a;
}

When I complied this program, the compiler displayed an error message: in line 2, '__int64' followed by 'long' is illegal.

But the strange thing is that there is no "__int64" in my code, only "int64_t", but "int64_t" and "__int64" are spelled differently, so they are totally different variables. Yes, it is wired that an error about a variable which never appears in my code occurred. What is wrong about line 2, and how does this line cause this error?

Thank you for reading and hope to receive your valuable answers.

  • 2
    What are you defining `int64_t` twice? `typedef long long long long int;` is indeed illegal, what do you expect to happen? – Quimby Jun 15 '23 at 05:52
  • 2
    marco (line 1) is process before the compilation, it will simply replace every `int64_t` by `long long int`. So, has @Quimby said, you go `long long long long int;` which is illegal. – Martin Morterol Jun 15 '23 at 05:59
  • Macros a simple text-replacements in the code. With your macro the statement `typedef long long int64_t;` will become `typedef long long long long int;`. Which doesn't make much sense. – Some programmer dude Jun 15 '23 at 06:01
  • With that said, I'm not sure you're allowed to redefine the *standard* symbol `int64_t`. C and C++ might be different in this case though (which is why you should almost never tag multiple *different* languages). – Some programmer dude Jun 15 '23 at 06:02
  • 1
    `long long long long int;` That'd be one big mother of a integer. I'm hoping for at least 256 bits. – user4581301 Jun 15 '23 at 06:11
  • 1
    Why do you manually define `int64_t`? Include `` (or `` in C) to let the compiler define it. – HolyBlackCat Jun 15 '23 at 07:16
  • There is nothing wrong about line 2. The problem is line 1 and line 2 together. Based on your previous question about a similar situation, I would guess that you don't know how macros work. – molbdnilo Jun 15 '23 at 07:18
  • Did the answer solve your problem? – Minxin Yu - MSFT Jun 16 '23 at 06:26

1 Answers1

8

Back before the C++ standard added long long as a standard type with a minimum of 64 bits, Microsoft added a type named __int64 to their compiler to provide the same basic capability, but with a name that was a conforming extension to C (i.e., names starting with an underscore followed by another underscore or an upper-case letter are reserved for the implementation).

When the C++ committee got around to adding long long to C++, Microsoft seems to have implemented it as an alias for their existing __int64 type. So, when you use long long, the compiler internally "thinks" of that as meaning __int64.

In your code, after the macro is expanded, you have:

typedef long long long long int;

In parsing this the compiler treats the first long long as meaning __int64. That's then followed by another long, and parsing fails. When it prints out the error message, it prints out long long using its internal name for that 64-bit type (__int64).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111