Here is a simple example to quickly understand the usage of ## pre-processing operator. It is used to merge two tokens into one, I mean two strings into one, which is refereed as token pasting or token concatenation. When a macro
is expanded, the two tokens on either side of each
Example code:
Code:
Output:
Note: Copying the code directly into your source.c file will also copies the invisible characters and finally you will left out with stray errors, i recommend you to type the code and that even becomes a practice.
For more details check this link
Please give your comments it will be encouraging for me :)
##
operator
are combined into a single token, which then replaces the ##
and
the two original tokens in the macro expansion.Example code:
Code:
/* * main.c file */ #include <stdio.h> #define MY_DEVICE_BASE_ADDR 0x40004000 #define MY_DEVICE_REG_CTRL_OFS 0x20 #define MY_DEVICE_REG_STATUS_OFS 0x30 #define MY_DEVICE_REG_INTR_OFS 0x40 #define REG(name) \ ((unsigned int)(MY_DEVICE_BASE_ADDR) + MY_DEVICE_REG_##name##_OFS) int main() { printf("Example to use ## Pre-Processor!\n"); printf(" *** My-Device Register ***\n"); printf(" - Control-Register Addr = 0x%x\n", REG(CTRL)); printf(" - Status-Register Addr = 0x%x\n", REG(STATUS)); printf(" - Interrupt-Register Addr = 0x%x\n", REG(INTR)); printf("[END]\n"); return 0; }
Output:
Example to use ## Pre-Processor! *** My-Device Register *** - Control-Register Addr = 0x40004020 - Status-Register Addr = 0x40004030 - Interrupt-Register Addr = 0x40004040 [END]
Note: Copying the code directly into your source.c file will also copies the invisible characters and finally you will left out with stray errors, i recommend you to type the code and that even becomes a practice.
For more details check this link
Please give your comments it will be encouraging for me :)
No comments:
Post a Comment