[ create a new paste ] login | about

Link: http://codepad.org/RkZsKLrv    [ raw code | output | fork ]

C, pasted on Dec 19:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int mscanf(int n, char *buf, char *format,...)
{
  char *p;
  va_list argp;
  va_start(argp, format);
  int rc;
  if ((p = buf) == NULL) {
    if ((buf = malloc(sizeof(char) * n)) == NULL)
      return -1;
  }
  fgets(buf, n, stdin);
  rc = vsscanf(buf, format, argp);
  if (p == NULL)
    free(p);
  return rc;
}

#define N 80
int main()
{
  static char buff[N];
  int a, b, c;
  printf("> ");
  printf("%d: ",mscanf(N, buff, "%d %d %d", &a, &b, &c));
  printf("a = %d, b = %d, c = %d\n", a, b, c);
  printf("> ");
  printf("%d: ",mscanf(N, NULL, "%d %d %d", &a, &b, &c));
  printf("a = %d, b = %d, c = %d\n", a, b, c);
  return 0;
}
/* end */


Output:
1
2
> -1: a = 134514544, b = 134513902, c = 1073830340
> 0: a = 134514544, b = 134513902, c = 1073830340


Create a new paste based on this one


Comments: