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 [email protected]
* 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= %dn inum2= %dn", inum1, inum2);
swap(int, inum1, inum2);
printf("nIntegers after swap:n inum1= %dn inum2= %dn", inum1, inum2);

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

printf("nEnter two Strings:n");
scanf("%s%s",ch1,ch2);
printf("n Strings before swap:n ch1= %sn ch2 = %sn", ch1, ch2);
swap(char *, ch1, ch2);
printf("nStrings after swap:n ch1= %sn ch2= %sn", 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 Follow @c_program

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) www.c-program-example.com

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 [email protected]
* 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: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", 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] = '';
}

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 Follow @c_program

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) www.c-program-example.com

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 [email protected]
* 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] = '';

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] = '';
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 Follow @c_program

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) www.c-program-example.com

K & R C Programs Exercise 4-10.

K and R C, Solution to Exercise 4-10:
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-5, An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. revise the caluculater to use this aproach. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<ctype.h>

#define MAXLINE 100
#define NUMBER '0' //SIGNAL THAT A NUMBER WAS FOUND
int getline(char line[], int limit);
int li = 0;
char line[MAXLINE];
/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
int i;
int c;

if(line[li] == '')
if(getline(line, MAXLINE) == 0)
return EOF;
else li = 0;
/* Skip whitespace */
while((s[0] = c = line[li++]) == ' ' || c == 't')
{
;
}
s[1] = '';

/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != '.' )
return c;
i = 0;

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

if(c == '.')
while(isdigit(s[++i] = c =line[i++]))
;
s[i] = '';
li--;
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 Follow @c_program

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) www.c-program-example.com

K & R C Programs Exercise 4-9.

K and R C, Solution to Exercise 4-9:
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-8, getch and ungetch do not handlea pushed back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement the design
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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp = 0;

//getch: get a (possibly pushed back) character
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
//ungetch: push a character back onto the input
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters!");
else
buf[bufp++] = 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 Follow @c_program

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) www.c-program-example.com

K & R C Programs Exercise 4-6.

K and R C, Solution to Exercise 4-6:
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 Add commands for handling variables.(It’s easy to provide twenty-six variables with single-letter names.). Add a variable for the most recently printed value.

/***********************************************************
* 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXOP 100
#define NUMBER 0
#define IDENTIFIER 1
#define ENDSTRING 2
#define TRUE 1
#define FALSE 0
#define MAX_ID_LEN 32
#define MAXVARS 30


struct varType{
char name[MAX_ID_LEN];
double val;
};


int Getop(char s[]);
void push(double val);
double pop(void);
void showTop(void);
void duplicate(void);
void swapItems(void);
void clearStacks(struct varType var[]);
void dealWithName(char s[], struct varType var[]);
void dealWithVar(char s[], struct varType var[]);

int pos = 0;
struct varType last;

int main(void)
{
int type;
double op2;
char s[MAXOP];
struct varType var[MAXVARS];

clearStacks(var);

while((type = Getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case IDENTIFIER:
dealWithName(s, var);
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop()- op2);
break;
case '/':
op2 = pop();
if(op2)
push(pop() / op2);
else
printf("nError: division by zero!");
break;
case '%':
op2 = pop();
if(op2)
push(fmod(pop(), op2));
else
printf("nError: division by zero!");
break;
case '?':
showTop();
break;
case '#':
duplicate();
break;
case '~':
swapItems();
break;
case '!':
clearStacks(var);
break;
case 'n':
printf("nt%.8gn", pop());
break;
case ENDSTRING:
break;
case '=':
pop();
var[pos].val = pop();
last.val = var[pos].val;
push(last.val);
break;
case '<':
printf("The last variable used was: %s (value == %g)n",
last.name, last.val);
break;

default:
printf("nError: unknown command %s.n", s);
break;
}
}
return EXIT_SUCCESS;
}

#define MAXVAL 100

int sp = 0;
double val[MAXVAL];

/* push: push f onto stack. */
void push(double f)
{
if(sp < MAXVAL)
val[sp++] = f;
else
printf("nError: stack full can't push %gn", f);
}

/*pop: pop and return top value from stack.*/
double pop(void)
{
if(sp > 0)
{
return val[--sp];
}
else
{
printf("nError: stack emptyn");
return 0.0;
}
}

void showTop(void)
{
if(sp > 0)
printf("Top of stack contains: %8gn", val[sp-1]);
else
printf("The stack is empty!n");
}


void duplicate(void)
{
double temp = pop();

push(temp);
push(temp);
}

void swapItems(void)
{
double item1 = pop();
double item2 = pop();

push(item1);
push(item2);
}


void clearStacks(struct varType var[])
{
int i;
sp = 0;

for( i = 0; i < MAXVARS; ++i)
{
var[i].name[0] = '';
var[i].val = 0.0;
}
}

void dealWithName(char s[], struct varType var[])
{
double op2;

if(!strcmp(s, "sin"))
push(sin(pop()));
else if(!strcmp(s, "cos"))
push(cos(pop()));
else if (!strcmp(s, "exp"))
push(exp(pop()));
else if(!strcmp(s, "pow"))
{
op2 = pop();
push(pow(pop(), op2));
}

else
{
dealWithVar(s, var);
}
}


void dealWithVar(char s[], struct varType var[])
{
int i = 0;

while(var[i].name[0] != '' && i < MAXVARS-1)
{
if(!strcmp(s, var[i].name))
{
strcpy(last.name, s);
last.val = var[i].val;
push(var[i].val);
pos = i;
return;
}
i++;
}

/* variable name not found so add it */
strcpy(var[i].name, s);
/* And save it to the last variable */
strcpy(last.name, s);
push(var[i].val);
pos = i;
}

int getch(void);
void unGetch(int);

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

/* Skip whitespace */
while((s[0] = c = getch()) == ' ' || c == 't')
{
;
}
s[1] = '';

if(isalpha(c))
{
i = 0;
while(isalpha(s[i++] = c ))
{
c = getch();
}
s[i - 1] = '';
if(c != EOF)
unGetch(c);
return IDENTIFIER;
}

/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != '.' && c != '-')
{
/* 4-6 Deal with assigning a variable. */
if('=' == c && 'n' == (next = getch()))
{
unGetch('');
return c;
}
if('' == c)
return ENDSTRING;

return c;
}

if(c == '-')
{
next = getch();
if(!isdigit(next) && next != '.')
{
return c;
}
c = next;
}
else
{
c = getch();
}

while(isdigit(s[++i] = c))
{
c = getch();
}
if(c == '.') /* Collect fraction part. */
{
while(isdigit(s[++i] = c = getch()))
;
}
s[i] = '';
if(c != EOF)
unGetch(c);
return NUMBER;
}

#define BUFSIZE 100

int buf[BUFSIZE];
int bufp = 0;

/* Getch: get a ( possibly pushed back) character. */
int getch(void)
{
return (bufp > 0) ? buf[--bufp]: getchar();
}

/* unGetch: push character back on input. */
void unGetch(int c)
{
if(bufp >= BUFSIZE)
printf("nUnGetch: too many charactersn");
else
buf[bufp++] = 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 Follow @c_program

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) www.c-program-example.com