Jan 17, 2012

K & R C Programs Exercise 5-1.

K and R C, Solution to Exercise 5-1:
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 get next integer from input into space, so that getint treats a + or - not followed by a digit as a valid representation of zero and fix it to push such a character back on the input. 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<ctype.h>

int getch(void);
void ungetch(int);

/* getint:  get next integer from input into *pn */
int getint(int *pn)
{
 int c, sign, sawsign;

 while (isspace(c = getch()))   /* skip white space */
  ;
 if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
  ungetch(c);    /* it's not a number */
  return 0;
 }
 sign = (c == '-') ? -1 : 1;
 if (sawsign = (c == '+' || c == '-'))
  c = getch();
 if (!isdigit(c)) {
  ungetch(c);
  if (sawsign)
   ungetch((sign == -1) ? '-' : '+');
  return 0;
 }
 for (*pn = 0; isdigit(c); c = getch())
  *pn = 10 * *pn + (c - '0');
 *pn *= sign;
 if (c != EOF)
  ungetch(c);
 return c;
}
Read more Similar 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

Jan 16, 2012

K & R C Programs Exercise 4-14.

K and R C, Solution to Exercise 4-14:
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.
Write C Program to swap two arguments using macros.
C Program to swap(t, x, y) that interchanges two arguments of type t using the block structure.

/***********************************************************
* 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 <stdio.h>

#define swap(t, x, y) \
  do { \
   t safe ## x ## y; \
   safe ## x ## y = x; \
   x = y; \
   y = safe ## x ## y; \
  } while (0)

int main(void) {
 int inum1, inum2;
 double dnum1, dnum2;
 char *ch1, *ch2;
 printf("\nEnter two Intgers:\n");
 scanf("%d%d",&inum1,&inum2);
 printf("\nIntegers before swap:\n inum1= %d\n inum2= %d\n", inum1, inum2);
 swap(int, inum1, inum2);
 printf("\nIntegers after swap:\n inum1= %d\n inum2= %d\n", inum1, inum2);

 printf("\nEnter two Doubles:\n");
 scanf("%f%f",&dnum1,&dnum2);
 printf("\nDoubles before swap:\n dnum1= %g\n dnum2= %g\n", dnum1, dnum2);
 swap(double, dnum1, dnum2);
 printf("\nDoubles after swap:\n dnum1= %g\n dnum2= %g\n", dnum1, dnum2);

 printf("\nEnter two Strings:\n");
 scanf("%s%s",ch1,ch2);
 printf("\n Strings before swap:\n ch1= %s\n ch2 = %s\n", ch1, ch2);
 swap(char *, ch1, ch2);
 printf("\nStrings after swap:\n ch1= %s\n ch2= %s\n", ch1, ch2);

 return 0;
}
Read more Similar 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

K & R C Programs Exercise 4-13.

K and R C, Solution to Exercise 4-13:
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.
Write a C Program to reverse the string using the recursive methods.
In this program reverse determines the length of the string and then calls the reverser, which reverses the string s in place. 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<stdio.h>
/* reverse: reverse the string s in place */
void reverse(char s[])
{

 void reverser(char s[], int i, int len);
 reverser(s,0,strlen(s));
}

/* reverser: reverse string s in place recursive */
void reverser(char s[], int i, int len)
{
 int c, j;
 j = len - (i + 1);
 if(i < j)
 {
  c = s[i];
  s[i] = s[j];
  s[j] = c;
  reverser(s, ++i, len);
 }
}

Read more Similar 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

K & R C Programs Exercise 4-12.

K and R C, Solution to Exercise 4-12:
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.
Write a C program to convert an integer into a string by calling a recursive routine.Recursive routine is the programming technique that a routine invoking itself again and again. 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<math.h>
void itoa(int n, char s[]);
int main(void) {
 char buffer[20];

 //for testing!
 printf("INT_MIN: %d\n", INT_MIN);
 itoa(INT_MIN, buffer);
 printf("Buffer : %s\n", buffer);

 return 0;
}
/* itoa: convert n to characters in s; recursive */
void itoa(int n, char s[])
{
 static int i;
 if(n / 10)
  itoa(n /10, s);
 else{
  i = 0;
  if(n < 0)
   s[i++] = '-';
 }
 s[i++] = abs(n) %10 + '0';
 s[i] = '\0';
}

Read more Similar 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

K & R C Programs Exercise 4-11.

K and R C, Solution to Exercise 4-11:
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 modify the K & R C Programs Exercise 4-3, Modify the gettop function so that it doesn't need to use ungetch by using internal static variable. 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<stdio.h>
#include<ctype.h>
#define NUMBER '0'

int getch(void);

/* getop: get next operator or numeric operand. */
int getop(char s[])
{
 int i ;
 int c;
 static int lastc = 0;

 if(lastc == 0)
  c = getch();
 else{
  c = lastc;
  lastc = 0;
 }

 while((s[0] = c) == ' ' || c == '\t')
  c = getch();
 s[1] = '\0';

 if(!isdigit(c) && c != '.')
  return c;              
 i = 0;
 if(isdigit(c))
  while(isdigit(s[++i] = c = getch()))
   ;
 if(c == '.')

  while(isdigit(s[++i] = c = getch()))
   ;

 s[i] = '\0';
 if(c != EOF)
  lastc = c;
 return NUMBER;

}
Read more Similar 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

C Program to find sum and product using macros.

Write C Program to find sum and product using macros.
Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.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<stdio.h>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
 int num1, num2, sum, product;
 printf("\nEnter the Two numbers\n");
 scanf("%d%d",&num1,&num2);
 sum=SUM(num1,num2);
 product=PROD(num1,num2);
 printf("\n\nSum of two numbers using Macros is:%d\n",sum);
 printf("\n\nProduct of two numbers using macros is:%d\n",product);
 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System
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