Ten things I love && hate about C
blog.micropledge.com
blog.micropledge.com
This is not very elegant, and you have to split your version number to major and minor part, but it does the job. Oh, yes, minor version must be between 0 and 99 (inclusive).
#define VERSION_MAJOR 2
#define VERSION_MINOR 9
#define VERSION_INT (VERSION_MAJOR * 100 + VERSION_MINOR)
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#if VERSION_MINOR<10
#define VERSION_STR STRINGIFY(VERSION_MAJOR) ".0" STRINGIFY(VERSION_MINOR)
#else
#define VERSION_STR STRINGIFY(VERSION_MAJOR) "." STRINGIFY(VERSION_MINOR)
#endif
#include <stdio.h>
#define VERSION_INT 3209
#if VERSION_INT < 100
#error VERSION_INT less than 100
#elif VERSION_INT < 1000
#define VERSION_STR (char[]){VERSION_INT/100+'0','.',(VERSION_INT%100)/10+'0',(VERSION_INT%10)+'0',0
#elif VERSION_INT < 10000
#define VERSION_STR (char[]){VERSION_INT/1000+'0',(VERSION_INT%1000)/100+'0','.',(VERSION_INT%100)/1
#endif
int main(void)
{
printf("%d\n", VERSION_INT);
printf("%s\n", VERSION_STR);
return 0;
}
Output: 3209
32.09
I'm surprised he doesn't mention this. I get magnitudes of speed (10000%) improvement over other languages after porting them to C.
The logic isn't 100% airtight - it is possible to argue that benefits of C are sufficient to take the performance hit over assembly, but not so great to take the hit from C to Ruby.