Post History
Use pointer arithmetic and \0 with string literals In C, strings are just zero-terminated character arrays, which means you can play tricks with pointer arithmetic. For example, the line char*s=a...
Answer
#1: Initial revision
# Use pointer arithmetic and `\0` with string literals In C, strings are just zero-terminated character arrays, which means you can play tricks with pointer arithmetic. For example, the line ``` char*s=a<2?"":"s"; ``` can be replaced with ``` char*s="s"+(a<2); ``` Note that you also can use zero bytes inside string constants, which may also help saving a few bytes. For example, if `n` is a number from 0 to 3, instead of: ``` char*s[]={"zero","one","two","three"};puts(s[n]); ``` you can write ``` char*s="zero\0.one\0..two\0..three";puts(s+6*n); ``` Note that the dots are just dummy characters so that the actual strings start at multiples of 6 characters. One may get rid of them at the cost of more complicated arithmetic: ``` char*s="zero\0one\0two\0three";puts(s+4*n+!!n); ``` Whether you save more characters in the string literal than adding to the expression of course depends on the specific strings (and on how often you use that expression in the code). Note also that if it is the only use of `s`, you may shorten the code further, since unlike array definitions, string literals can be used inline: ``` puts("zero\0one\0two\0three"+4*n+!!n); ```