ArticleslgStudy

computer science

M4 (computer language)

M4 (computer language) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand M4 (computer language) rather than just read about it. In short: m4 is a general-purpose macro processor included in most Unix-like operating systems, and is a component of the POSIX standard. The language was designed by Brian Kernighan and Dennis Ritchie for the original versions of UNIX.

Key takeaways

  • M4 (computer language) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect M4 (computer language) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of M4 (computer language) from memory before moving on to harder problems.

Reference excerpt

m4 is a general-purpose macro processor included in most Unix-like operating systems, and is a component of the POSIX standard. The language was designed by Brian Kernighan and Dennis Ritchie for the original versions of UNIX. It is an extension of an earlier macro processor, m3, written by Ritchie for an unknown AP-3 minicomputer. The macro preprocessor operates as a text-replacement tool. It is employed to re-use text templates, typically in computer programming applications, but also in text editing and text-processing applications. Most users require m4 as a dependency of GNU autoconf and GNU Bison.

History Macro processors became popular when programmers commonly used assembly language. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors at the time. In the 1960s, an early general-purpose macro processor, M6, was in use at AT&T Bell Laboratories, which was developed by Douglas McIlroy, Robert Morris and Andrew Hall. Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey. The distinguishing features of this style of macro preprocessing included:

free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing) the high degree of re-expansion (a macro's arguments get expanded twice: once during scanning and once at interpretation time) The implementation of Rational Fortran used m4 as its macro engine from the beginning, and most Unix variants ship with it. As of 2024 many applications continue to use m4 as part of the GNU Project's autoconf. It also appears in the configuration process of sendmail (a widespread mail transfer agent) and for generating footprints in the gEDA toolsuite. The SELinux Reference Policy relies heavily on the m4 macro processor. m4 has many uses in code generation, but (as with any macro processor) problems can be hard to debug.

Features m4 offers these facilities:

a free-form syntax, rather than line-based syntax a high degree of macro expansion (arguments get expanded during scan and again during interpretation) text replacement parameter substitution file inclusion string manipulation conditional evaluation arithmetic expressions system interface programmer diagnostics programming language independent human language independent provides programming language capabilities Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor dialect of Fortran. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language. Unquoted identifiers which match defined macros are replaced with their definitions. Placing identifiers in quotes suppresses expansion until possibly later, such as when a quoted string is expanded as part of macro replacement. Unlike most languages, strings in m4 are quoted using the backtick (`) as the starting delimiter, and apostrophe (') as the ending delimiter. Separate starting and ending delimiters allows the arbitrary nesting of quotation marks in strings to be used, allowing a fine degree of control of how and when macro expansion takes place in different parts of a string.

Example The following fragment gives a simple example that could form part of a library for generating HTML code. It defines a commented macro to number sections automatically:

Processing this code with m4 generates the following text:

Implementations FreeBSD, NetBSD, and OpenBSD provide independent implementations of the m4 language. Furthermore, the Heirloom Project Development Tools includes a free version of the m4 language, derived from OpenSolaris. M4 has been included in the Inferno operating system. This implementation is more closely related to the original m4 developed by Kernighan and Ritchie in Version 7 Unix than its more sophisticated relatives in UNIX System V and POSIX. GNU m4 is an implementation of m4 for the GNU Project. It is designed to avoid many kinds of arbitrary limits found in traditional m4 implementations, such as maximum line lengths, maximum size of a macro and number of macros. Removing such arbitrary limits is one of the stated goals of the GNU Project. The GNU Autoconf package makes extensive use of the features of GNU m4. GNU m4 is currently maintained by Gary V. Vaughan and Eric Blake. GNU m4 is free software, released under the terms of the GNU General Public License.

See also AWK – text processing programming language C preprocessor Macro (computer science) Make Template processor Web template system

References

External links

GNU m4 website GNU m4 manual m4 tutorial Macro Magic: m4, Part One and Part Two

Worked examples

Example 1 — a first encounter with M4 (computer language)

Start with the simplest possible case. Write down what M4 (computer language) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to M4 (computer language) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about M4 (computer language) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of M4 (computer language)

In research
M4 (computer language) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses M4 (computer language) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
M4 (computer language) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Inferno (operating system) commands, Macro programming languages, Programming languages standardized by POSIX, so understanding it makes those chapters shorter.
In everyday life
Look for M4 (computer language) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “M4 (computer language)” →

How to study M4 (computer language) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what M4 (computer language) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain M4 (computer language) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is M4 (computer language) in simple terms?

m4 is a general-purpose macro processor included in most Unix-like operating systems, and is a component of the POSIX standard. The language was designed by Brian Kernighan and Dennis Ritchie for the original versions of UNIX.

Why does M4 (computer language) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study M4 (computer language)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on M4 (computer language).

Tags

  • Inferno (operating system) commands
  • Macro programming languages
  • Programming languages standardized by POSIX

Keep exploring