Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a define inside a format string?

Tags:

People also ask

How do you format a string in Python?

There are several ways to format output. To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

What is the use of format string?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.


Say I have a character array:

#define LEN 10
char arr[LEN + 1];

Lets do some scanf operation to it:

scanf("Name: %s", arr);

This could be dangerous if someone is typing a name that is longer than 10 characters. So better use this:

scanf("Name: %10s", arr);

Well now I would run into trouble if LEN is changed. I would have to go through the whole code to correct every line where I used the 10 in context of arr. So I thought about somehting like this:

scanf("Name: %LENs", arr);

But this will not work.LEN is not resolved by the preprocessor beacuse it is used inside a string.

How to use a define inside a format string?