What is ${} ing called?

When we use ${} to display the value of any variable amidst text, what is the process called? Is it substituting or referencing?

Also, is there any such sign we can use in C++ while writing a program, inside the cout text?

Example:

int x=23;
cout<<"They have prepared ${x} cakes with honey and butter.";

Result: They have prepared 23 cakes with honey and butter.

Thanks.

It’s printing, though the scope is more limited to a single variable containing single or short string in this context.


Are we talking about ChoiceScript or C++ itself? I’m sure ${} is non-entity in C++.

I am talking about C++. Is there any substitute in C++ to use instead of ${} .

It’s been a long time since my last practice using C++, so a bit rusty here, but AFAI-can-tell that’s not how you print a text with variable between them.

int x=23;
cout << "They have prepared" << int << "cakes with honey and butter.";

https://www.w3schools.com/cpp/cpp_variables.asp


Otherwise, the good 'ol way

cout << "They have prepared";
cout << int;
cout << "cakes with honey and butter.";

Is there no way to print the variable directly inside the “text” part of cout without using <<int<<?

No. It’s part of C++ protocol. Keeping it short, C++ (and many other coding languages) uses a very specific syntax when handling certain datatypes.

Okay. Thanks.

This is an in-line string formatting. Most high level programming languages have this feature.

I don’t know much about C++, but in python it would be something like this:

var = 23
print(f"They have prepared {var} cakes with honey and butter.")

# or you could do the old way (before Python 3):

print("They have prepared {} cakes with honey and butter.".format(var))

In this post in StackOverflow, they use it like this for C++ 20:

#include <iostream>
#include <format>

int main() {
    std::cout << std::format("Hello {}!\n", "world");
}

So I guess to do what you want it would be something like:

#include <iostream>
#include <format>

int main() {
    int x=23;
    std::cout << std::format("They have prepared {} cakes with honey and butter.", x);
}

But don’t take my word for it.

1 Like

I will try making a demo program with that. Even if it doesn’t work, thank you for taking the time.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.