Implementing Code Instances
Program code:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h>/***** cplusplus *****/
#if 0 #include <iostream> using namespace std; #endif//Get a byte or Word #define MEM_B (x) (* (Byte) (x)) #define MEM_W (x) (* (Word *) (x))//Get The offset of a field in the structure body (struct) #define FPOS (Type, field) ((DWORD) & ((type *) 0)->field)//Convert an alphabetic character to uppercase #de Fine UPCASE (c) ((c) >= ' A ' && (c) <= ' Z ')? ((c) -0x20): (c))//To determine whether the character is a decimal digit #define DECCHECK (c) ((c) >= ' 0 ' && (c) <= ' 9 ')//judge whether the character is 16 Digital #define HEXCHECK (hex) ((hex) >= ' 0 ' && (hex) <= ' 9 ') | |
((hex) >= ' A ' && (hex) <= ' F ') | |
((hex) >= ' a ' && (hex) <= ' F ')) One way to prevent overflow #define INC_SAT (val) (Val= (val) +1 > (val))? (val) +1: (val))//count the number of array elements #define ARRAY_SIZE (array) (sizeof (array)/sizeof (array[0))
int main (void) {int x = 0X1234ABCD;
char c = ' a ';
char Dec = ' 5 ';
Char hex = ' e ';
Char array[10] = {' 1 '};
printf ("Mem_b (x): 0x%p/n", Mem_b (x));
printf ("Mem_w (x): 0x%p/n", Mem_w (x));
printf ("UPCASE (c):%c->%c/n", C, UPCASE (c));
printf ("Deccheck (dec):%c->%d/n", Dec, Deccheck (DEC));
printf ("Hexcheck (hex):%c->%d/n", Hex, Hexcheck (hex));
printf ("Array_size (array): array[10]->%d/n", Array_size (array));
printf ("/n/****** MACRO ******//n");
printf ("__line__:%d/n", __line__);
printf ("__file__:%s/n", __file__);
printf ("__date__:%s/n", __date__);
printf ("__time__:%s/n", __time__);
printf ("__func__:%s/n", __func__);
#ifdef __cplusplus cout << "Hello __cplusplus" <<endl;
#endif #ifdef __stdc__ printf ("Hello __stdc__/n"); #endif printf ("/n/****** SizeoF () ******//n ");
printf ("sizeof (byte):%d/n", sizeof (byte));
printf ("sizeof (char):%d/n", sizeof (char));
printf ("sizeof (signed char):%d/n", sizeof (signed char));
printf ("sizeof (unsigned char):%d/n", sizeof (unsigned char));
printf ("sizeof (short):%d/n", sizeof (short));
printf ("sizeof (signed short):%d/n", sizeof (signed short));
printf ("sizeof (unsigned short):%d/n", sizeof (unsigned short));
printf ("sizeof (int):%d/n", sizeof (int));
printf ("sizeof (signed int):%d/n", sizeof (signed int));
printf ("sizeof (unsigned int):%d/n", sizeof (unsigned int));
printf ("sizeof (long):%d/n", sizeof (long));
printf ("sizeof (signed long):%d/n", sizeof (signed long));
printf ("sizeof (unsigned long):%d/n", sizeof (unsigned long));
printf ("sizeof (Long Long):%d/n", sizeof (long Long));
printf ("sizeof (signed Long Long):%d/n", sizeof (signed long Long)); printf ("sizeof" (unsigned lOng long):%d/n ", sizeof (unsigned long long));
printf ("sizeof (float):%d/n", sizeof (float));
printf ("sizeof (double):%d/n", sizeof (double));
Exit (exit_success); }