Feb 7, 2012

K & R C Programs Exercise 7-3.

K and R C, Solution to Exercise 7-3:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to Revise minprintf to handle more of the other facilities of printf. minprintf walks along the argument list and printf does the actual printing for the facilities supported.
To handle more of the other facilities of printf we collect in local fmt the % and any other characters until an alphabetic character -the format letter.local fmt is the format argument for printf. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include <stdarg.h>
#include <stdio.h>
#include<ctype.h>
#define LOCALFMT 100

/* minprintf:  minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
 va_list ap;
 char *p, *sval;
 char localfmt[LOCALFMT];
 int i;
 int ival;
 double dval;
 unsigned uval;

 va_start(ap, fmt);    /* make ap point to the first unnamed arg */
 for (p = fmt; *p; p++) {
  if (*p != '%') {
   putchar(*p);
   continue;
  }
  i = 0;
  localfmt[i++] = '%';
  while(*p(p+1) && !isalpha(*(p+1)))
   localfmt[i++] = *++p;
  localfmt[i++] = *(p+1);
  localfmt[i] = '/0';
  switch (*++p) {
  case 'd':
  case 'i':
   ival = va_arg(ap, int);
   printf("%d", ival);
   break;
  case 'c':
   ival = va_arg(ap, int);
   putchar(ival);
   break;
  case 'u':
   uval = va_arg(ap, unsigned int);
   printf("%u", uval);
   break;
  case 'o':
   uval = va_arg(ap, unsigned int);
   printf("%o", uval);
   break;
  case 'x':
   uval = va_arg(ap, unsigned int);
   printf("%x", uval);
   break;
  case 'X':
   uval = va_arg(ap, unsigned int);
   printf("%X", uval);
   break;
  case 'e':
   dval = va_arg(ap, double);
   printf("%e", dval);
   break;
  case 'f':
   dval = va_arg(ap, double);
   printf("%f", dval);
   break;
  case 'g':
   dval = va_arg(ap, double);
   printf("%g", dval);
   break;
  case 's':
   for (sval = va_arg(ap, char *); *sval; sval++)
    putchar(*sval);
   break;
  default:
   putchar(*p);
   break;
  }
 }
 va_end(ap);
}




Read more c programs
C Basic
C Strings
K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link