ch1-Count characters in input

Chapter_1     Exercise_1-7     copyEOF Exercise_1-8







CONTENTS:     count.c     countf.c




count.c     K&R, p. 18         download


#include <stdio.h> // for getchar(), printf(), EOF
// count characters in input
int main()
{
long nc = 0;

while (getchar() != EOF)
{nc++;}

printf("%ld\n", nc);
}
/*
gcc count.c -o count
./count // input from keyboard
Hello, guys!
Have a nice day!
// Ctrl+D in Linux, Ctrl+Z then Enter in Windows
30 // 12 + 1 + 16 + 1 // 1 for newline

./count < count.c // source file
434

./count < count // binary file
16744
*/











countf.c     K&R, p. 18         download


#include <stdio.h> // for getchar(), printf(), EOF
// count characters in input (floating-point version)
int main()
{
double nc; // float nc;

for (nc = 0; getchar() != EOF; nc++)
{}

printf("%.0f\n", nc); // 0 fraction digits
}
/*
gcc countf.c -o countf
./countf // input from keyboard
Hello, guys!
Have a nice day!
// Ctrl+D in Linux, Ctrl+Z then Enter in Windows
30 // 12 + 1 + 16 + 1 // 1 for newline

./countf < countf.c // source file
496

./countf < countf // binary file
16008
*/









Chapter_1     Exercise_1-7     copyEOF BACK_TO_TOP Exercise_1-8



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo