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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With