encyclopegia.org

X macro

X macros are an idiomatic usage of programming language macros for generating list-like structures of data or code.[src] They are most useful when at least some of the lists cannot be composed by indexing, such as at compile time.[src] They provide reliable maintenance of parallel lists whose corresponding items must be declared or executed in the same order.[src] Examples of such lists include initializing arrays, along with declaring enumeration constants and function prototypes, generating statement sequences and switch arms, etc.[src] Usage of X macros dates back to the 1960s.[src] They remain useful in modern-day C and C++, yet are still relatively unknown.[src]

X macros are an idiomatic usage of programming language macros for generating list-like structures of data or code.[src] They are most useful when at least some of the lists cannot be composed by indexing, such as at compile time.[src] They provide reliable maintenance of parallel lists whose corresponding items must be declared or executed in the same order.[src] Examples of such lists include initializing arrays, along with declaring enumeration constants and function prototypes, generating statement sequences and switch arms, etc.[src] Usage of X macros dates back to the 1960s.[src] They remain useful in modern-day C and C++, yet are still relatively unknown.[src] ==Implementation== An X macro application consists of two parts: The definition of the list's elements.[src] Expansion(s) of the list to generate fragments of declarations or statements.[src] The list is defined by a macro or header file (named, LIST ) which generates no code by itself, but merely consists of a sequence of invocations of a macro (classically named " X ") with the elements' data.[src] Each expansion of LIST is preceded by a definition of X with the syntax for a list element.[src] The invocation of LIST expands X for each element in the list.[src] ===Example 1=== This example defines a list of variables, and automatically generates their declarations and a function to print them out.[src] First the list definition.[src] The list entries could contain multiple arguments, but here only the name of the variable is used.[src] define LIST_OF_VARIABLES \ X(value1) \ X(value2) \ X(value3) Then the list is expanded to generate the variable declarations: define X(name) int name; LIST_OF_VARIABLES undef X In a similar way, we can generate a function that prints the variables and their values: void variables() { define X(name) printf("%s = %d\n", #name, name); LIST_OF_VARIABLES undef X } When run through the C preprocessor, the following code is generated.[src] Line breaks and indentation have been added for ease of reading, even though they are not actually generated by the preprocessor: int value1; int value2; int value3; void variables() { printf("%s = %d\n", "value1", value1); printf("%s = %d\n", "value2", value2); printf("%s = %d\n", "value3", value3); } define FOR_LIST_OF_VARIABLES(DO) \ DO(id1, name1) \ DO(id2, name2) \ DO(id3, name3) As above, execute this list to generate the variable declarations: define DEFINE_NAME_VAR(id, name, ...) int name; FOR_LIST_OF_VARIABLES( DEFINE_NAME_VAR ) or declare an enumeration: define DEFINE_ENUMERATION(id, name, ...) name = id, enum IdListType { FOR_LIST_OF_VARIABLES( DEFINE_ENUMERATION ) }; In a similar way, we can generate a function that prints the variables and their names: void variables() { #define PRINT_NAME_AND_VALUE(id, name, ...) printf(#name " = %d\n", name); FOR_LIST_OF_VARIABLES( PRINT_NAME_AND_VALUE ) } ==Further reading== Variadic macro ==References==[src]

22 sentences · 22 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"X macro." encyclopegia.org, 2026, https://encyclopegia.org/wiki/x-macro. Accessed 27 November 2026.

Chicago

"X macro." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/x-macro.

APA

encyclopegia.org. (2026). X macro. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/x-macro

Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Canon MP-E 65mm f/2.8 1–5x Macro lens

stub · references available

Macro Markets

stub · references available

Macro Express

stub · references available

Category:User Music Macro Language-1

stub · references available

Macro-Puinavean

stub · references available

Olympus Zuiko Digital 35mm f/3.5 Macro

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1366969011 · 0.5 · snapshot enwiki-001366969011

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.