S
sathya
Q. Write a program to print all input lines that are longer than 80
characters.
Code taken from
http://users.powernet.co.uk/eton/kandr2/krx117.html
========================code===========================
#include <stdio.h>
#define MINLENGTH 81
int readbuff(char *buffer) {
size_t i=0;
int c;
while (i < MINLENGTH) {
c = getchar();
if (c == EOF) return -1;
if (c == '\n') return 0;
buffer[i++] = c;
}
return 1;
}
int copyline(char *buffer) {
size_t i;
int c;
int status = 1;
for(i=0; i<MINLENGTH; i++)
putchar(buffer);
while(status == 1) {
c = getchar();
if (c == EOF)
status = -1;
else if (c == '\n')
status = 0;
else
putchar(c);
}
putchar('\n');
return status;
}
int main(void) {
char buffer[MINLENGTH];
int status = 0;
while (status != -1) {
status = readbuff(buffer);
if (status == 1)
status = copyline(buffer);
}
return 0;
}
================================code====================
My doubts are:
1)Why there are two getchar functions in copyline and readbuff. Can it
be done with one
global buffer pointer?
2) status variable in the main function is either 1 ,0,-1. I have seen
codes uses this kind of checking a function’s return type. That is
initializing the variable at first either with 0 or 1 and then checking
the modified value of that variable with function return type. Does this
practice carry any name?
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)
characters.
Code taken from
http://users.powernet.co.uk/eton/kandr2/krx117.html
========================code===========================
#include <stdio.h>
#define MINLENGTH 81
int readbuff(char *buffer) {
size_t i=0;
int c;
while (i < MINLENGTH) {
c = getchar();
if (c == EOF) return -1;
if (c == '\n') return 0;
buffer[i++] = c;
}
return 1;
}
int copyline(char *buffer) {
size_t i;
int c;
int status = 1;
for(i=0; i<MINLENGTH; i++)
putchar(buffer);
while(status == 1) {
c = getchar();
if (c == EOF)
status = -1;
else if (c == '\n')
status = 0;
else
putchar(c);
}
putchar('\n');
return status;
}
int main(void) {
char buffer[MINLENGTH];
int status = 0;
while (status != -1) {
status = readbuff(buffer);
if (status == 1)
status = copyline(buffer);
}
return 0;
}
================================code====================
My doubts are:
1)Why there are two getchar functions in copyline and readbuff. Can it
be done with one
global buffer pointer?
2) status variable in the main function is either 1 ,0,-1. I have seen
codes uses this kind of checking a function’s return type. That is
initializing the variable at first either with 0 or 1 and then checking
the modified value of that variable with function return type. Does this
practice carry any name?
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)