Hacker News new | past | comments | ask | show | jobs | submit login

Because I can't post on stackoverflow either, here's my variant in C (requires glibc, and a deprecated function at that).

  #include <printf.h>
  #include <stdio.h>
  #include <string.h>

  int d_cb(FILE *stream, const struct printf_info *info, const void *const* args) {
    int num = (*(int*)(((int**)args)[0])), numw = info->width;
    char str[10], numn = num|numw, *out = str;
    num = num < 0 ? *out++ = '-', -num : numn;
    do { *out++ = '0' + num % 10; num /= 10; } while(num);
    fwrite(str, out - str, 1, stream);
    return 0;
  }

  void init() {
    register_printf_function('d', d_cb, 0);
  }

  int main() {
    init();
    printf("2+4 = %d, 2+2 = %1d\n", 2+4, 2+2);
    return 0;
  }



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: