# What is 'unsigned char near'?

I read a datasheet and I found variable defined as unsigned char near Sample_X. What is this, and how does this differ from unsigned char Sample_X?

• perhaps allows the compiler to use small relative offsets? Apr 16, 2018 at 11:41
• Based on part experience with x86 in DOS, I would expect this with some pointer type and in a 16bit environment. Yet what the OP quoted does not look like a pointer and the datasheet link would imply some MCU. Search for the keyword "near" in the following two links: microchip.com/forums/m549709.aspx barrgroup.com/Embedded-Systems/How-To/Efficient-C-Code
– frr
Apr 16, 2018 at 11:58
• It should be noted that this is a compiler extensions, not standard C Apr 16, 2018 at 13:03
• FYI, the keyword, near, is an example of a storage class specifier. It tells the compiler something about how or where the storage for the variable should be allocated. (See filo's answer, below, for more about near). Apr 16, 2018 at 14:25
• Is this really on-topic here? Apr 16, 2018 at 23:26

The MCU specified here is a Freescale MC9C08 series, which uses a slightly enhanced version of their HC08 architecture. This is an 8-bit core, which (like many such) has shorter instructions and faster access to "zero page" addresses than others. Zero-page addresses are only 8 bits long instead of 16 bits, so instructions referencing them can be 2 bytes instead of 3, and take 1 cycle less to execute as a result.

The "near" keyword instructs the compiler to put the variable in "zero page" if possible, for better performance. Compilers specifically written for embedded development, such as this one, usually implement such extensions to the language (here described as "C Support for Zero Page").

• I like this answer, but do you have a source for your information? Apr 17, 2018 at 0:37
• I edited the answer to provide more detail and references. In fact it's from Freescale's newer HCS08 architecture, but this is code-compatible with the older HC08 and HC05 architectures. The (now positively ancient) 6800 and 6502 architectures are also closely related. Apr 17, 2018 at 1:27
• Nice, much better! Apr 17, 2018 at 1:30
• So, its usage is very similar to the register keyword (for cases where you are really sure that this is where you need optimization), but slightly less extreme?
– vsz
Apr 17, 2018 at 4:24
• More or less. The difference is mainly due to the fact that 8-bit CPUs typically don't have a register bank per se, only a single accumulator and a couple of index registers. But it's also analogous to the use of "near" vs. "far" pointers on x86, in that there is a difference in the size of the address and how long it takes to complete an access. Apr 17, 2018 at 13:13

Depending on the CPU architecture there may be different instructions to access data at different address. Here is an example from Keil for one of their compilers.

The near access has a certain memory limit, so you may give hints to the compiler to place some frequently used variables in an area that is accessible by shorter instructions (obviously describing access to an 32-bit address space is larger than 16-bit address space). This can translate to smaller/faster code.

• There's more information in What is the difference between far pointers and near pointers? Apr 16, 2018 at 11:50
• @AndrewMorton: That question relates to far and near pointers on the largely-obsolete 8086 architecture. The same keywords are used in related but different fashion on 8-bit microcontrollers. Apr 16, 2018 at 16:02
• @supercat The question just asks generally, although the answers focus on the old architecture. Someone knowledgeable should write an answer on it explaining how they are used for 8bit microcontrollers. Apr 17, 2018 at 3:20
• @curiousdannii: The code in question was written for an 8-bit micro. Apr 17, 2018 at 13:57