K & R C Programs Exercise 5-18.

K and R C, Solution to Exercise 5-18:
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 convert the C declaration into a word description and Make dcl recover from input errors.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<string.h>
#include<ctype.h>
enum { NAME, PARENS, BRACKETS};
enum { NO, YES};
void dcl(void);
void dirdcl(void);
void errmsg(char *);


int gettoken(void);
extern int tokentype;
extern char token[];
extern char name[];
extern char out[];
extern int prevtoken;

main()
{
int type;
char temp[MAXTOKEN];
while (gettoken() !=EOF) {
strcy(out, token);
while ((type = gettoken()) != 'n')
if(type == PARENS || type == BRACKETS)
strcat(out, token);
else if(type == '*') {
sprintf(temp, "(*%s)", out);
strcpy(out, temp);
}else if(type == NAME){
sprintf(temp, "%s%s",token, out);
strcpy(out, temp);
}else
printf("Invalid input at %sn",token);
printf("%sn",out);
}
return 0;
}


//dcl:parse a declarator
void dcl(void)
{
int ns;
for(ns = 0; gettoken() == '*';)
ns++;
dirdcl();
while(ns --> 0)
strcat(out, "pointer to");
}


//dirdcl: parse a direct declaration
void dirdcl(void)
{
int type;
if(tokentype == '('){
dcl();
if(tokentype != ')')
errmsg("error:missimg)n");
}
else if(tokentype == NAME)
strcpy(name,token);
else
errmsg("error:expected name or (dcl)n");
while((type = gettoken()) == PARENS || type == BRACKETS)
if(type == PARENS)
strcat(out, "function returning");
else {
strcat(out, "array");
strcat(out, token);
strcat(out, "of");
}
}

//errmsg: prints the error message
void errmsg(char *msg)
{
printf("%sn",msg);
prevtoken = YES;
}


//get token:return next token
int gettoken(void)
{
int c, getch(void);
void ungetch(int);
char *p = token;
if(prevtoken == YES) {
prevtoken = NO;
return tokentype;
}
while((c = getch()) == ' ' || c == 't')
;
if(c == '('){
if ((c = getch()) == ')'){
strcpy(token,"()");
return tokentype = PARENS;
}
else{
ungetch(c);
return tokentype = '(';
}
}
else if(c == '['){
for(*p++ = c; (*p++ = getch()) != ']';)
;
*p = '';
return tokentype = BRACKETS;
}
else if (isalpha(c)) {
for(*p++ = c; isalnum(c = getch());)
*p++ = c;
*p = '';
ungetch(c);
return tokentype = NAME;
} else
return tokentype = c;
}


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 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 5-12.

K and R C, Solution to Exercise 5-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.
C program extend to entab and detab( written as in the K and R Exercise 5-11) to accept the short hand entab -m +n
to mean tab stops every n coloumns, starting at column m. 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 MAXLINE 100
#define TABINC 8
#define YES 1
#define NO 0
void esettab(int argc , char *argv[], char *tab);
void entab(char *tab);
void detab(char *tab);
int tabpos(int pos, char *tab);

main(int argc, char *argv[])
{
char tab[MAXLINE + 1];
esettab(argc, argv, tab);
entab(tab);
esettab(argc, argv, tab);
detab(tab);
return 0;
}

/*entab: replace strings of blanks with tabs and blanks */
void entab(char *tab)
{
int c, pos;
int nb = 0;
int nt = 0;
for(pos = 1;(c=getchar()) != EOF;pos++)

if(c == ' '){
if(tabpos(pos, tab) == NO)
++nb;
else{
nb = 0;
++nt;
}
}else {
for(;nt > 0;nt--)
putchar('t');
if (c == 't')
nb = 0;
else
for(;nb > 0;nb--)
putchar(' ');
putchar(c);
if(c == 'n')
pos = 0;
else if(c == 't')
while (tabpos(pos(pos, tab) != YES)
++pos;
}
}

/*detab:replace tab with blanks*/
void detab(char *tab)
{
int c pos = 1;
while ((c = getchar()) != EOF)
if (c == 't') {
do
putchar(' ');
while (tabpos(pos++, tab) != YES);
}else if(c == 'n'){
putchar(c);
pos = 1;
}else{
putchar(c);
++pos;
}
}


//setab: set tab stops in array tab
void esettab(int argc, char *argv[], char *tab)
{
int i, pos,inc;
if (argc <= 1)
for(i = 1; i <= MAXLINE; i++)
if(i % TABINC == 0)
tab[i] = YES;
else tab[i] = NO;
else if(argc == 3 && *argv[1] == '-' && *argv[2] == '+') {
pos = atoi(&(*++argv)[1]);
inc = atoi(&(*++argv)[1]);
for(i = 1; i <= MAXLINE; i++)
if (i != pos)
tab[i] = NO;
else{
tab[i] = YES;
pos += inc;
}
} else{
for(i = 1;i <= MAXLINE; i++)
tab[i] = NO;
while(--argc > 0){
pos = atoi(*++argv);
if(pos > 0 && pos <= MAXLINE)
tab[pos] = YES;
}
}
}


//tabpos: determine if pos is at a tab stop
int tabpos(int pos, char *tab)
{
if (pos > MAXLINE)
return YES;
else
return tab[pos];
}

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 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 5-11.

K and R C, Solution to Exercise 5-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 entab and detab(written as exercises in chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments. 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 MAXLINE 100
#define TABINC 8
#define YES 1
#define NO 0
void settab(int argc , char *argv[], char *tab);
void entab(char *tab);
void detab(char *tab);
int tabpos(int pos, char *tab);

main(int argc, char *argv[])
{
char tab[MAXLINE + 1];
settab(argc, argv, tab);
entab(tab);
settab(argc, argv, tab);
detab(tab);
return 0;
}

/*entab: replace strings of blanks with tabs and blanks */
void entab(char *tab)
{
int c, pos;
int nb = 0;
int nt = 0;
for(pos = 1;(c=getchar()) != EOF;pos++)

if(c == ' '){
if(tabpos(pos, tab) == NO)
++nb;
else{
nb = 0;
++nt;
}
}else {
for(;nt > 0;nt--)
putchar('t');
if (c == 't')
nb = 0;
else
for(;nb > 0;nb--)
putchar(' ');
putchar(c);
if(c == 'n')
pos = 0;
else if(c == 't')
while (tabpos(pos(pos, tab) != YES)
++pos;
}
}

/*detab:replace tab with blanks*/
void detab(char *tab)
{
int c,pos = 1;
while ((c = getchar()) != EOF)
if (c == 't') {
do
putchar(' ');
while (tabpos(pos++, tab) != YES);
}else if(c == 'n'){
putchar(c);
pos = 1;
}else{
putchar(c);
++pos;
}
}


//setab: set tab stops in array tab
void settab(int argc, char *argv[], char *tab)
{
int i, pos;
if (argc <= 1)
for(i = 1; i <= MAXLINE; i++)
if(i % TABINC == 0)
tab[i] = YES;
else tab[i] = NO;
else{
for(i = 1;i <= MAXLINE; i++)
tab[i] = NO;
while(--argc > 0){
pos = atoi(*++argv);
if(pos > 0 && pos <= MAXLINE)
tab[pos] = YES;
}
}
}


//tabpos: determine if pos is at a tab stop
int tabpos(int pos, char *tab)
{
if (pos > MAXLINE)
return YES;
else
return tab[pos];
}

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 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 5-7.

K and R C, Solution to Exercise 5-7:
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 rewrite the readline function to store lines in the arraysupplied by main, rather than calling alloc to maintain storage. How much faster is the program?
The readlile function is slightly faster than the original version in the book K and R C Program page 109.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 <string.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1
#define FALSE 0

#define MAXLINES 5000 /* maximum number of lines */
#define MAXLEN 1000 /* maximum length of a line */
char *lineptr[MAXLINES];
char lines[MAXLINES][MAXLEN];


int getline(char s[], int lim)
{
int c, i;

for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != 'n'; i++)
s[i] = c;
if (c == 'n') {
s[i++] = c;
}
s[i] = '';
return i;
}


int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];

nlines = 0;
while ((len = getline(line, MAXLEN)) > 0)
if (nlines >= maxlines || (p = malloc(len)) == NULL)
return -1;
else {
line[len - 1] = ''; /* delete the newline */
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}

int readlines2(char lines[][MAXLEN], int maxlines)
{
int len, nlines;

nlines = 0;
while ((len = getline(lines[nlines], MAXLEN)) > 0)
if (nlines >= maxlines)
return -1;
else
lines[nlines++][len - 1] = '';
return nlines;
}

int main(int argc, char *argv[])
{

readlines2(lines, MAXLINES);

if (argc > 1 && *argv[1] == '2') {
puts("readlines2()");
readlines2(lines, MAXLINES);
} else {
puts("readlines()");
readlines(lineptr, MAXLINES);
}

return 0;
}

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 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 5-6.

K and R C, Solution to Exercise 5-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 Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4). 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>

/* getline: get line into s, return length */
int getline(char *s, int lim)
{
char *p;
int c;

p = s;
while (--lim > 0 && (c = getchar()) != EOF && c != 'n')
*p++ = c;
if (c == 'n')
*p++ = c;
*p = '';
return (int)(p - s);
}


int atoi(char *s)
{
int n, sign;

while (isspace(*s))
s++;
sign = (*s == '+' || *s == '-') ? ((*s++ == '+') ? 1 : -1) : 1;
for (n = 0; isdigit(*s); s++)
n = (n * 10) + (*s - '0');
return sign * n;
}

/*The itoa() function converts an integer value into an
ASCII string of digits.*/

char *utoa(unsigned value, char *digits, int base)
{
char *s, *p;

s = "0123456789abcdefghijklmnopqrstuvwxyz";
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < (unsigned) base) {
digits[0] = s[value];
digits[1] = '';
} else {
for (p = utoa(value / ((unsigned)base), digits, base);
*p;
p++);
utoa( value % ((unsigned)base), p, base);
}
return digits;
}

char *itoa(int value, char *digits, int base)
{
char *d;
unsigned u;

d = digits;
if (base == 0)
base = 10;
if (digits == NULL || base < 2 || base > 36)
return NULL;
if (value < 0) {
*d++ = '-';
u = -((unsigned)value);
} else
u = value;
utoa(u, d, base);
return digits;
}


static void swap(char *a, char *b, size_t n)
{
while (n--) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
a++;
b++;
}
}

void my_memrev(char *s, size_t n)
{
switch (n) {
case 0:
case 1:
break;
case 2:
case 3:
swap(s, s + n - 1, 1);
break;
default:
my_memrev(s, n / 2);
my_memrev(s + ((n + 1) / 2), n / 2);
swap(s, s + ((n + 1) / 2), n / 2);
break;
}
}

void reverse(char *s)
{
char *p;

for (p = s; *p; p++)
;
my_memrev(s, (size_t)(p - s));
}



static char *strchr(char *s, int c)
{
char ch = c;

for ( ; *s != ch; ++s)
if (*s == '')
return NULL;
return s;
}

int strindex(char *s, char *t)
{
char *u, *v, *w;

if (*t == '')
return 0;
for (u = s; (u = strchr(u, *t)) != NULL; ++u) {
for (v = u, w = t; ; )
if (*++w == '')
return (int)(u - s);
else if (*++v != *w)
break;
}
return -1;
}


#define NUMBER '0'

int getop(char *s)
{
int c;

while ((*s = c = getch()) == ' ' || c == 't')
;
*(s + 1) = '';
if (!isdigit(c) && c != '.')
return c; /* not a number */
if (isdigit(c)) /* collect integer part */
while (isdigit(*++s = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(*++s = c = getch()))
;
*++s = '';
if (c != EOF)
ungetch(c);
return NUMBER;
}




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 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 5-4.

K and R C, Solution to Exercise 5-4:
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, that returns 1 if the string t occurs at the end of the string s, and zero otherwise. 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>

//finds the string length, standard "strlen" function
int str_len(char *s)
{
int n;

for(n = 0; *s != ''; s++)
{
n++;
}
return n;
}

int str_cmp(char *s, char *t)
{
for(;*s == *t; s++, t++)
if(*s == '')
return 0;
return *s - *t;
}


int str_end(char *s, char *t)
{
int Result = 0;
int s_length = 0;
int t_length = 0;

/* get the lengths of the strings */
s_length = str_len(s);
t_length = str_len(t);

/* check if the lengths mean that the string t could fit at the string s */
if(t_length <= s_length)
{
/* advance the s pointer to where the string t would have to start in string s */
s += s_length - t_length;

/* and make the compare using strcmp */
if(0 == str_cmp(s, t))
{
Result = 1;
}
}

return Result;
}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
char Str3[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("n Enter the third string: n");
scanf("%s",Str3);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);
printf("String two is (%s)n", Str3);

if(str_end(Str1, Str2))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str2);
}
if(str_end(Str1, Str3))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str3);
}



return 0;
}


C BasicC StringsK and R C Programs ExerciseYou 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 5-3.

K and R C, Solution to Exercise 5-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 concatenate the two strings using the pointers
This program shows, how the standard library function “strcat” works!. 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>
/* str_cat: concatenate t to the end of s: pointer version */
void str_cat(char *s, char *t)
{
/* run through the destination string until we point at the terminating '' */
while('' != *s)
{

++s;
}

/* now copy until we run out of string to copy */
while('' != (*s = *t))
{
++s;
++t;
}

}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);

str_cat(Str1, Str2);
printf("The combined string is (%s)n", Str1);

return 0;
}


C BasicC StringsK and R C Programs ExerciseYou 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 5-2.

K and R C, Solution to Exercise 5-2:
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 floating point analog of getint.The routine getfloat is similar to the routine getint. getfloat skips whitespaces, records the sign, and stores the integer part of the number at the address in fp.
getfloat also handles the practional part of the number(but not scientific notation). the fractional part is added to *fp in the same fashion as the integer part. 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<ctype.h>
#include<math.h>

int getfloat(float *fp)
{
int ch;
int sign;
int fraction;
int digits;

while (isspace(ch = getch())) /* skip white space */
;

if (!isdigit(ch) && ch != EOF && ch != '+'
&& ch != '-' && ch != '.') {
ungetch(ch);
return 0;
}

sign = (ch == '-') ? -1 : 1;
if (ch == '+' || ch == '-') {
ch = getch();
if (!isdigit(ch) && ch != '.') {
if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return 0;
}
}
}

*fp = 0;
fraction = 0;
digits = 0;
for ( ; isdigit(ch) || ch == '.' ; ch = getch()) {
if (ch == '.') {
fraction = 1;
} else {
if (!fraction) {
*fp = 10 * *fp + (ch - '0');
} else {
*fp = *fp + ((ch - '0') / pow(10, fraction));
fraction++;
}
digits++;
}
}

*fp *= sign;

if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return (digits) ? ch : 0;
}
}

//for testing... try the different one!
#include<stdio.h>

int main(void)
{
int ret;

do {
float f;

fputs("Enter a number: ", stdout);
fflush(stdout);
ret = getfloat(&f);
if (ret > 0) {
printf("You entered: %fn", f);
}
} while (ret > 0);

if (ret == EOF) {
puts("Stopped by EOF.");
} else {
puts("Stopped by bad input.");
}

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