#define-like behavior

Paul Backus snarwin at gmail.com
Tue Mar 14 06:25:51 UTC 2023


On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote:
> Hi, in C and C++ you can use #define to substitute a value in 
> place of an identifier while preprocessing. If you initialize a 
> new string and don't change its value after that, will the 
> compiler substitute the string identifier with its value, like 
> #define in C, or will it make a string in memory and refer to 
> that?

In D, you can get #define-like behavior by declaring the string 
as a [manifest constant][1], like this:

```d
enum myString = "hello";
```

For more tips on how to translate C preprocessor idioms to D, 
take a look at ["The C Preprocessor vs D"][2] in the ["Articles" 
section][3] of dlang.org.

[1]: https://dlang.org/spec/enum.html#manifest_constants
[2]: https://dlang.org/articles/pretod.html
[3]: https://dlang.org/articles/index.html


More information about the Digitalmars-d-learn mailing list