Usage of ## in C language

Publisher:独行于世Latest update time:2015-02-10 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Today I read the Linux operating system source code and found this section:
                                        #define _syscall0(type,name) \
                                        type name(void) \
                                        { \
                                        long __res; \
                                        __asm__ volatile ( "int $0x80" \ // Call system interrupt 0x80.
                                        :"=a" (__res) \ // Return value??eax(__res).
                                        :"" (__NR_
                                        ##name)); \ // Input is the system interrupt call number __NR_name.
                                         if (__res >= 0) \ // If the return value >= 0, return the value directly.
                                          return (type) __res; errno = -__res; \ // Otherwise set the error number and return -1.
                                          return -1;} 
There are two '#' signs in one place. I don't understand what they mean. I found a forum post on the Internet:
      Usage of "#" and "##" in macros 

1. General usage 
We use # to turn the macro parameter into a string, and use ## to paste two macro parameters together. 
Usage: 
#include 
#include 
using namespace std; 

#define STR(s) #s 
#define CONS(a,b) int(a##e##b) 

int main() 

    printf(STR(vck)); // Output string "vck" 
    printf("%d 
", CONS(2,3)); // 2e3 output: 2000 
    return 0; 


2. When the macro parameter is another macro, 
it should be noted that the macro parameter will not be expanded if there is '#' or '##' in the macro definition. 

1. Non-'#' and '##' cases 
#define TOW (2) 
#define MUL(a,b) (a*b) 

printf("%d*%d=%d 
", TOW, TOW, MUL(TOW,TOW)); 
This line of macro will be expanded to: 
printf("%d*%d=%d 
", (2), (2), ((2)*(2))); 
The parameter TOW in MUL will be expanded to (2). 

2, when there is '#' or '##' 
#define A (2) 
#define STR(s) #s 
#define CONS(a,b) int(a##e##b) 

printf("int max: %s 
", STR(INT_MAX)); // INT_MAX #include 
This line will be expanded to: 
printf("int max: %s 
", "INT_MAX"); 

printf("%s 
", CONS(A, A)); // compile error  
This line is: 
printf("%s 
", int(AeA)); 

Neither INT_MAX nor A will be expanded, but the solution to this problem is very simple. Add an extra layer of intermediate conversion macro. 
The purpose of adding this layer of macro is to expand all macro parameters in this layer, so that the macro (_STR) in the conversion macro can get the correct macro parameters. 

#define A (2) 
#define _STR(s) #s 
#define STR(s) _STR(s) // Conversion macro 
#define _CONS(a,b) int(a##e##b) 
#define CONS(a,b) _CONS(a,b) // Conversion macroprintf 

("int max: %s 
", STR(INT_MAX)); // INT_MAX, the maximum value of int type, is a variable #include 
Output: int max: 0x7fffffff 
STR(INT_MAX) --> _STR(0x7fffffff) Then convert to a string; 

printf("%d 
", CONS(A, A)); 
Output: 200 
CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2)) 

III. Some special cases of '#' and '##' 
1. Merge anonymous variable names 
#define ___ANONYMOUS1(type, var, line) type var##line 
#define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line) 
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__) 
Example: ANONYMOUS(static int); That is: static int _anonymous70; 70 represents the line number; 
First level: ANONYMOUS(static int); --> __ANONYMOUS0(static int, __LINE__); 
Second level: --> ___ANONYMOUS1(static int, _anonymous, 70); 
Third level: --> static int _anonymous70; 
That is, only the macro of the current layer can be unlocked each time, so __LINE__ can be unlocked only in the second layer; 

2. Filling structure 
#define FILL(a) {a, #a} 

enum IDD{OPEN, CLOSE};
typedef struct MSG{ 
  IDD id; 
  const char * msg; 
}MSG; 

MSG _msg[] = {FILL(OPEN), FILL(CLOSE)}; 
is equivalent to: 
MSG _msg[] = {{OPEN, "OPEN"}, 
              {CLOSE, "CLOSE"}}; 

3. Record file name 
#define _GET_FILE_NAME(f) #f 
#define GET_FILE_NAME(f) _GET_FILE_NAME(f) 
static char FILE_NAME[] = GET_FILE_NAME(__FILE__); 

4. Get the string buffer size corresponding to a numerical type 
#define _TYPE_BUF_SIZE(type) sizeof #type 
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type) 
char buf[TYPE_BUF_SIZE(INT_MAX)]; 
     --> char buf[_TYPE_BUF_SIZE(0x7fffffff)]; 
     --> char buf[sizeof "0x7fffffff"]; 
here is equivalent to: 
char buf[11];
 I wrote a program under Linux:
#include
#define transform 1##2##3
int main()
{
    int result=transform*transform;
    printf("the num of result is : %d",result);
    return 0;

The function output is 15129 (=123*123) - the conclusion is: one of the functions of ## is to connect the two macro parameters together!

Reference address:Usage of ## in C language

Previous article:Embedded problem (codewarrior compilation, download program)
Next article:KEILC51 compile ERROR L104

Recommended ReadingLatest update time:2024-11-15 23:36

07-S3C2440 driver learning (I) Embedded Linux character device driver-LED character device driver
1. Embedded Linux character device driver framework People who write applications should not look at circuit diagrams, but how to operate the hardware: calling open, read, write, etc. in the driver program to achieve it. The C library implements the open, read, and write upper-level functions Call open, etc.: swi
[Microcontroller]
07-S3C2440 driver learning (I) Embedded Linux character device driver-LED character device driver
ARM-LINUX study notes
    After installing the ssh service yesterday, I tried to log in with xshell on Windows today and found that I couldn't log in. The reason was that I used the NAT mode of VirtualBox. In NAT mode, the client can access the Internet easily, but if you want to connect to the host, you need to turn on the network address
[Microcontroller]
ARM-LINUX study notes
The growth path of single-chip microcomputer (51 basic chapters) - 006 Building a development and programming environment under Linux
There is no easy-to-use IDE like Keli to develop 51 microcontrollers under Linux, so the development environment can only be built by yourself.  Step 1: Install cross-compilation tools  a) Install SDCC  sudo apt-get install sdcc  b) Test whether SDCC is available. This is a simple running light code test.c found on
[Microcontroller]
The growth path of single-chip microcomputer (51 basic chapters) - 006 Building a development and programming environment under Linux
Linux that greatly simplifies AMP configuration and use
Embedded systems generally fall into two categories: those that require hard real-time performance, and those that don't. In the past, we've had to make the difficult choice between the performance of a real-time operating system or the rich features of our beloved Linux system, and then work hard to make up for the s
[Microcontroller]
Linux that greatly simplifies AMP configuration and use
Linux 2.6 kernel for embedded systems applications
With the rapid development of information technology combining multimedia technology and communication technology and the widespread application of the Internet, the PC era has also transitioned to the post-PC era. In the post-PC era with the rapid development of digital information technology and network technology,
[Microcontroller]
Compiling Mplayer under Linux - S3C6410
mplayer cross-compilation process: GCC version: Sourcery G++ 4.2.1 Target machine: S3C6410 ARM core is arm1176jzf-s (GCC has -mcpu=arm1176jzf-s) Host: Ubuntu 8.04 Tips: mplayer has automatic configure, no need to write Make manually~ Pay attention to the difference between GCC parameters and confi
[Microcontroller]
05-S3C2440 learning kernel (porting) linux3.4.2 porting (1) simple porting + modifying MTD partition + making jffs2 file system
1. Framework introduction and simple modification 1.1 How does the Bootloader boot the kernel? Working of Bootloader: (1) Reading the kernel into memory (2) Store some kernel startup parameters in a specified location and parse them when the kernel starts (3) Start the kernel and pass in the machine ID 1.2. Ke
[Microcontroller]
05-S3C2440 learning kernel (porting) linux3.4.2 porting (1) simple porting + modifying MTD partition + making jffs2 file system
Design and implementation of wireless meteorological data communication system based on ARM-Linux
Automatic weather station data collectors are generally designed based on single-chip microcomputers or PC/104 bus controllers, and have the characteristics of good compatibility with PCs, low power consumption, and compact size. However, how to design an automatic weather station data collector with powerful function
[Microcontroller]
Design and implementation of wireless meteorological data communication system based on ARM-Linux
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号