C preprocessor: Difference between revisions

Content deleted Content added
 
(4 intermediate revisions by 2 users not shown)
Line 14:
}}</ref>
 
The [[C Sharp (programming language)|C#]] programming language also allows for directives, even though they are not read by a preprocessor and they cannot be used for creating macros, and isare generally more intended for features such as conditional compilation.<ref>{{cite web|url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives|title=C# preprocessor directives|date=14 January 2022 |publisher=Microsoft}}</ref> C# seldom requires the use of the directives, for example code inclusion does not require a preprocessor at all (as C# relies on a package/namespace system like Java, no code needs to be "included").
 
The [[Haskell]] programming language also allows the usage of the C preprocessor, which is invoked by writing <syntaxhighlight lang="Haskell" inline>{-# LANGUAGE CPP #-}</syntaxhighlight> at the top of the file. The accepted preprocessor directives align with those in standard C/C++.
 
Features of the preprocessor are encoded in [[source code]] as [[Directive (programming)|directives]] that start with <code>#</code>.
Line 52:
Until [[C++26]], the C++ keywords <code>import</code>, <code>export</code>, and <code>module</code> were partially handled by the preprocessor as well.
 
The Haskell programming language also accepts C preprocessor directives, which is invoked by writing <syntaxhighlight lang="Haskell" inline>{-# LANGUAGE CPP #-}</syntaxhighlight> at the top of the file. The accepted preprocessor directives align with those in standard C/C++.
Haskell also accepts C preprocessor directives.
 
=== C# ===
Line 133:
 
==== Binary resource inclusion ====
[[C23 (C standard revision)|C23]] and [[C++26]] introduce the <code>#embed</code> directive for '''binary resource inclusion''' which allows including the content of a binary file into a source even thoughif it's is not valid C code.<ref>{{cite web |title=WG14-N3017 : #embed – a scannable, tooling-friendly binary resource inclusion mechanism |url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm |website=open-std.org |archive-url=https://web.archive.org/web/20221224045304/https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm |archive-date=24 December 2022 |date=2022-06-27 |url-status=live}}</ref><ref>{{Cite web|title=#embed - a scannable, tooling-friendly binary resource inclusion mechanism|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1967r14.html|website=open-std.org }}</ref>
This allows binary resources (like images) to be included into a program without requiring processing by external tools like <code>xxd -i</code> and without the use of [[string literal]]s which have a length limit on [[MSVC]]. Similarly to <code>xxd -i</code> the directive is replaced by a comma separated list of integers corresponding to the data of the specified resource. More precisely, if an array of type {{code|unsigned char}} is initialized using an <code>#embed</code> directive, the result is the same as-if the resource was written to the array using <code>[[fread]]</code> (unless a parameter changes the embed element width to something other than <code>[[Limits.h|CHAR_BIT]]</code>). Apart from the convenience, <code>#embed</code> is also easier <!--than what?--> for compilers to handle, since they are allowed to skip expanding the directive to its full form due to the [[as-if rule]].
 
Line 139:
 
<syntaxhighlight lang="cpp">
const unsigned char icon_display_dataiconDisplayData[] = {
#embed "art.png"
};
 
/* specify any type which can be initialized form integer constant expressions will do */
const char reset_blobresetBlob[] = {
#embed "data.bin"
};
 
/* attributes work just as well */
const signed char aligned_data_stralignedDataString[] __attribute__ ((aligned (8))) = {
#embed "attributes.xml"
};
Line 468:
|title=An empirical analysis of c preprocessor use |journal=IEEE Transactions on Software Engineering |url = http://dl.acm.org/citation.cfm?id=631305
|date = December 2002
|volume=28 |issue=12 |pages=1146–1170 |doi=10.1109/TSE.2002.1158288 |bibcode=2002ITSEn..28.1146E |url-access=subscription }}</ref>
 
;Hidden multiple evaluation