The difference between "#" and "#" in macro definition is tested today. The result is as follows: "#" Indicates a connection to a string. "##" Indicates a connection with a symbol. the symbol can be a variable or another macro symbol. Example: Macro definition: (1) # Define dev_file_name "/dev/test_kft" # Define open_file (FD, N )/ {/ FD = open (dev_file_name # N, o_rdonly );/ If (FD <0 )/ {/ Printf ("open device error/N ");/ Return 0 ;/ }/ } So called: Open_file (fd1, 1 ); Open_file (fd2, 2 ); Open_file (FD3, 3 ); Open_file (fd4, 4 ); Open_file (fd5, 5 ); Open_file (fd6, 6 ); After expanding with GCC-E 2299: {fd1 = open ("/dev/test_kft", 00); If (fd1 <0) {printf ("open device error/N "); return 0 ;}}; 2300: {fd2 = open ("/dev/test_kft" "2", 00); If (fd2 <0) {printf ("open device error/N "); return 0 ;}}; 2301: {FD3 = open ("/dev/test_kft" "3", 00); If (FD3 <0) {printf ("open device error/N "); return 0 ;}}; 2302: {fd4 = open ("/dev/test_kft" "4", 00); If (fd4 <0) {printf ("open device error/N "); return 0 ;}}; 2303: {fd5 = open ("/dev/test_kft" "5", 00); If (fd5 <0) {printf ("open device error/N "); return 0 ;}}; 2304: {fd6 = open ("/dev/test_kft" "6", 00); If (fd6 <0) {printf ("open device error/N "); return 0 ;}}; If dev_file_name is not defined 2299: {fd1 = open (dev_file_name "1", 00); If (fd1 <0) {printf ("open device error/N"); Return 0 ;}}; 2300: {fd2 = open (dev_file_name "2", 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}}; 2301: {FD3 = open (dev_file_name "3", 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}}; 2302: {fd4 = open (dev_file_name "4", 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}}; 2303: {fd5 = open (dev_file_name "5", 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}}; 2304: {fd6 = open (dev_file_name "6", 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}}; So we can clearly see that # N is parsed as "N", used to connect an existing string. (2) Let's take a look at #. The macro definition is as follows: # Define open_file (FD, N )/ {/ FD = open (dev_file_name ## N, o_rdonly );/ If (FD <0 )/ {/ Printf ("open device error/N ");/ Return 0 ;/ }/ } The call method is the same. Watch the macro development: 2299: {fd1 = open (dev_file_name1, 00); If (fd1 <0) {printf ("open device error/N"); Return 0 ;}}; 2300: {fd2 = open (dev_file_name2, 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}}; 2301: {FD3 = open (dev_file_name3, 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}}; 2302: {fd4 = open (dev_file_name4, 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}}; 2303: {fd5 = open (dev_file_name5, 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}}; 2304: {fd6 = open (dev_file_name6, 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}}; Now we can see that # N is used to directly connect n to the end of a symbol. Well, now let's define a symbol to see the effect. # Define dev_file_name1 "/dev/test_kft1" Expand again: 2299: {fd1 = open ("/dev/test_kft1", 00); If (fd1 <0) {printf ("open device error/N "); return 0 ;}}; 2300: {fd2 = open (dev_file_name2, 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}}; 2301: {FD3 = open (dev_file_name3, 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}}; 2302: {fd4 = open (dev_file_name4, 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}}; 2303: {fd5 = open (dev_file_name5, 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}}; 2304: {fd6 = open (dev_file_name6, 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}}; Obviously, the first symbol is replaced because it is a macro. In this way, we can also apply this extension feature to variables. |