2
\$\begingroup\$

Let's take an example:

extern volatile __bit TRISB0 @ (((unsigned) &TRISB)*8) + 0;

I believe this is pointing TRISB0 to the appropriate bit address. But I don't understand why __bit is used instead of bit, the XC8 data type.

I've tried to find an explanation for __bit in the XC8 User's Guide and elsewhere, but to no avail.

\$\endgroup\$
1
  • \$\begingroup\$ I think double __ is used for reserved variables/names I think \$\endgroup\$
    – KyranF
    Oct 24, 2014 at 21:47

2 Answers 2

7
\$\begingroup\$

It's right there in the user guide in black and white...

bit and __bit are exactly the same, except for the level of compatibility:

If the xc8 flag --STRICT is used, the bit keyword becomes unavailable, but you can use the __bit keyword.

So you can use bit for your own data type, variable name, function name, whatever you want, if you specify --STRICT on the command line, and still use __bit to specify a bit.

The --STRICT option is used to enable strict ANSI C conformance of all special, non-standard keywords. The MPLAB XC8 C compiler supports various special keywords (for example the persistent type qualifier). If the --STRICT option is used, these keywords are changed to include two underscore characters at the beginning of the keyword (for example, __persistent) so as to strictly conform to the ANSI standard. Thus if you use this option, you will need to use the qualifier __persistent in your code, not persistent.

\$\endgroup\$
3
  • \$\begingroup\$ Can you tell me where you got the User's Guide? My version goes "... becomes unavailable." No mention of __bit. First thing I did was search the document for __bit. \$\endgroup\$ Oct 24, 2014 at 23:30
  • \$\begingroup\$ I just asked google for it, and it gave me Microchip's website: ww1.microchip.com/downloads/en/DeviceDoc/… \$\endgroup\$
    – Majenko
    Oct 25, 2014 at 0:24
  • \$\begingroup\$ Very good (don't know how I got an older version). \$\endgroup\$ Oct 25, 2014 at 1:22
4
\$\begingroup\$

This is probably more of a general programming question than an electronics question, but since this group encompasses embedded systems programming to some extent, I'll answer it here.

The C standard promises that code may use any syntactically-valid identifier without risk of conflict with any implementation-defined identifiers, provided that:

  1. The user code does not use #include <name> syntax for any headers not defined by the standard.
  2. The identifier does not conflict with any that are mentioned in the standard as being associated with headers included using the aforementioned syntax.
  3. The identifier does not start with two underscores.

A standards-conforming compiler must allow a C program to declare a variable, function, or type named bit. If a compiler itself were to define a bit type, but user code tried to assign some other meaning to that name, the two usages would conflict; standards-compliant compilers are not allowed to have such conflicts with standards-conforming code.

By using __bit rather than bit as the name for its own bit type, the compiler ensures that the name will not generate any impermissible conflict. While such a name might still conflict with a user-defined identifier of the same name, the standard would allow an implementation to do whatever it likes in case of a conflict involving such a name. Most compilers will refrain from doing anything totally evil (e.g. pretending to compile normally, but generating code which would do nothing but endlessly output "FRINK RULES!" to all attached UARTs), but the standard allows compiler authors to do whatever they want, with no restriction whatsoever, in case of such naming conflicts).

\$\endgroup\$
1
  • \$\begingroup\$ It's all so clear now. \$\endgroup\$ Oct 25, 2014 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.