<md>

# Formatting Literals

Jancy has three kinds of literals:

* C-literal;
* Hex-literal;
* Formatting literal.

The first one is the good old C-style literal. It defines a statically allocated const char array.

<div class='new_frame snippet'>
<code name="jancy">

char a [] = "hello world";

</code>
</div>
<br>

The second kind is the hex literal. This kind of literals allows for a nice and clean way of defining in-program const binary data blocks (i.e. icons, public keys etc) Just like C-literals, hex literals define a statically allocated const char array.

<div class='new_frame snippet'>
<code name="jancy">

char b [] = 0x"61 62 63 20 64 65 66 00";  
// same as: char b [] = { 0x61, 0x62, 0x63, 0x20, 0x64, 0x65, 0x66, 0x00  }

</code>
</div>
<br>

It's OK to use upper-case or lower-case and group hex codes with spaces to your liking (or not use spaces for grouping at all):

<div class='new_frame snippet'>
<code name="jancy">

char c [] = 0x"696a 6b6c 6D6E 6F70 0000";

</code>
</div>
<br>

Concatenating hex and C-literals can be used for removing trailing zeroes from C-literals thus producing non-zero-terminated literals:

<div class='new_frame snippet'>
<code name="jancy">

char d [] = "non-zero-terminated" 0x"";

</code>
</div>
<br>

The third and last kind of Jancy literals is the formatting literal. Literals of this kind bring Perl-style formatting into our C-family language. A formatting literal produces a dynamically allocated char array on the GC heap:

<div class='new_frame snippet'>
<code name="jancy">

int i = 100;	

char const* c = $"i = $i";

</code>
</div>
<br>

Jancy allows the use of expressions and printf-style formatting:

<div class='new_frame snippet'>
<code name="jancy">

int i = 100;	
char a [] = "hello world";
uint_t h = 0xbeef;

char const* c = $"i = $i; a [6] = $(a [6]; c); h = 0x$(h; 08x)";

</code>
</div>
<br>

It's also OK to specify some of injected values (or all of them) in the argument list of the formatting literal and reference these values by index. You can even reference the same value multiple times to display it using different format specifiers:

<div class='new_frame snippet'>
<code name="jancy">

char const* c = $"rgb dec = (%1, %2, %3); rgb hex = (%(1;x), %(2;x), %(3;x))" (
	(colorTable [i].m_value & 0xff0000) >> 16,
	(colorTable [i].m_value & 0x00ff00) >> 8,
	(colorTable [i].m_value & 0x0000ff)
	);

</code>
</div>
<br>

Last but not least, all literal kinds can be concatenated and combined. If the combination does not include formatting literals, then the result is a statically allocated const char array. If the combination includes formatting literals then it will produce a dynamically allocated char array on the GC heap:

<div class='new_frame snippet'>
<code name="jancy">

int i = 100;	
char a [] = "hello world";

char const* c = 
	0x"61 62 63" 
	" ...concatenated to... " 
	$"i = $i; a = $a; " 
	0x"64 65 66" 
	" ...end."
	);	

</code>
</div>

---
Proceed to Jancy [flag enums](fenums.html)
</md>
