Line counting program and EOF question

0 Toyen . · September 11, 2015
Hello there,

I am currently reading a book about C and there are some examples of simple programs in there like this:

int main()
{

int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);


    return 0;
}



First of all, why are there no curly brackets after the if and while statements? And this program is supposed to be counting input lines but no matter what i input, it does not seem to be doing anything.

Also, how do I know I have reached EOF? There was an exercise to make a program that would print the value of EOF and I had no idea how to do this.

I' d appreciate anyone who could clear this up for me a bit.

Thanks : )

Post a Reply

Replies

Oldest  Newest  Rating
0 Toyen . · September 12, 2015
Thanks a lot c student!

I have now successfully managed to input characters via keyboard and then I pressed CTRL+Z and the program counted the lines correctly!

However I still struggle with the file input using the code you suggested.

I have created a simple text file in the same folder where my program is but no matter where I put the command line, it says that the file is undeclared.

Where exactly should I put it?

Also I was wondering if you could take a look at this program - http://www.learntosolveit.com/cprogramming/Ex_1.9_SinBlank.html

And clear some stuff up for me a bit.

why exactly is NONBLANK defined as 'a' and why is lastc equal to it?

I dont really get the lastc part, how does the program know if lastc is equal to a blank space or not if the input function does not have anything to do with it?
0 c student · September 12, 2015
getchar reads the characters parsed into stdin.  by using file redirection, it is unnecessary to open the file in the program.  this program isnt limited to file processing, you can run your program normally and type in whatever sequence of characters then pressing either Ctrl+D for *nix or Ctrl+Z for windows to send an EOF to end the while loop.
0 Toyen . · September 11, 2015
hey there!

I am getting an error saying that the file is undeclared, should I open the file using Buckys method?

I think all these simple programs are supposed to have a text file as the input to work with but it was never mentioned in the book or explained how to implement it to the code so I am super confused.

Example:

The next program counts characters; it is similar to the copy program.

#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}


So this program is supposed to be counting characters of a text file?
0 c student · September 11, 2015
statements don't require curly brackets unless you want to process more than one expression.  EOF is end of file and it is represented as an int of -1 which will be returned into the c variable.  whenever a newline character is returned into c, it will increment the nl variable.

try parsing a file into the program with the command line like so:
myprogram < myfile.txt

and it should print however many lines it has
  • 1

C

107,270 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator