This blog is under construction

Friday, 27 April 2012

vfprintf example in C

Header file:

     int vfprintf(FILE *stream, const char *format, va_list arguments);

    It is similar to fprintf, except that the variable argument list is given as arguments.

vfprintf function C example:


  void vprintf_impl(FILE *stream, char *format, ...) {
        va_list arg;
        va_start (arg, format);
        vfprintf (stream, format, arg);
        va_end (arg);

  int main() {
        char name[100], company[100], str[256];
        int age, salary;
        printf("Enter your name:\n");
        fgets(name, 90, stdin);
        name[strlen(name) - 1] = '\0';
        printf("Enter your designation:\n");
        fgets(company, 90, stdin);
        company[strlen(company) - 1] = '\0';
        printf("Enter your age and salary:\n");
        scanf("%d%d", &age, &salary);
        vprintf_impl(stdout, "%s works as %s earns %d per "
                "month at the age of %d\n", name, company, salary, age);
        return 0;

  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Enter your name:
  Enter your designation:
  Enter your age and salary:
  23 2000
  Thomas works as Engineer earns 2000 per month at the age of 23

No comments:

Post a Comment