0

I found this book have many people suggest for newbie, but some code on it doesn't work, although I code it exactly like the code in the book but it still don't work

#include <stdio.h>
main()
{
     int a ;
     for (a = 0; getchar() != EOF ; ++a);
     printf ("%d",a);
}

It looks like after the loop it ends immediately, code after the loop is not executed.

Is this book is too old? Is there any another book for self learning c programming?

Cœur
  • 37,241
  • 25
  • 195
  • 267
thomas
  • 11
  • 2
  • 1
    How are you doing `EOF`? – Gopi Sep 22 '15 at 12:29
  • 5
    `++a);` --> `++a)` remove `;` – BLUEPIXY Sep 22 '15 at 12:29
  • 3
    This is no valid C according to current standard (or C99). `main` does not have a correct signature and should be prototype-style. Get a more recent book! – too honest for this site Sep 22 '15 at 12:30
  • 4
    @BLUEPIXY why do you think so? let the loop run and print the value after it . Which gives count of number of times the loop ran – Gopi Sep 22 '15 at 12:30
  • what if you put `fflush(stdout);` after the printf? – UmNyobe Sep 22 '15 at 12:32
  • 1
    @UmNyobe: it should be enough to include a new-line in the format: `"%d\n"`. – M Oehm Sep 22 '15 at 12:33
  • 1
    @gopi Be you're right. – BLUEPIXY Sep 22 '15 at 12:33
  • 1
    How are you running this program? Does it open a console window and as soon as you press EOF (and enter) the window disappears? Please tell us more what happens when you run the program. – Some programmer dude Sep 22 '15 at 12:34
  • @UmNyobe: It should still print/flush on termination. OP should just inspect the console output carefully. There should be a vaule. Also he should state how he notices the loop is is not executed. – too honest for this site Sep 22 '15 at 12:35
  • That book is not exactly wrong, but to old. Get a c11book – Michi Sep 22 '15 at 12:36
  • 1
    @Olaf The book in question is K&R 2nd ed, which is explicitly for C89. Pointing out that C89 is not C99 is useless at best. – Jens Sep 22 '15 at 12:36
  • 2
    @Michi The book is one of the best you can find. And which C11 book is there? – Jens Sep 22 '15 at 12:37
  • @Jens: Where did I? (I know the book). I just wrote it is no valid C according to the **current** (and previous) version of the standard. You might have noted that C90 is not valid standard since C95. The current version of gcc, for instance, defaults to C11. Do you really think it is a good idea for a beginner to learn C90? – too honest for this site Sep 22 '15 at 12:38
  • @Olaf And how is that helpful for someone stating he is using a C89 book? One which is *still* considered the best book out there? Note that the problem is not an error message from a C99 compiler. This doesn't look like a C89 versus C99 problem, but rather one of omission or invalid expectations. – Jens Sep 22 '15 at 12:41
  • @gopi: EOF, see http://stackoverflow.com/questions/1118957/c-how-to-simulate-an-eof - in a new line type ^Z + return – outofmind Sep 22 '15 at 12:42
  • 1
    @Michi On the contrary, that book is almost certainly wrong, somewhere on almost every page. For example this code is taken from K&R 1.5.2, it doesn't have a return statement, which was _mandatory_ in C90. It invokes undefined behavior. Blatant newbie bug and will not compile on a C90 compiler. – Lundin Sep 22 '15 at 12:43
  • @Jens: Did you read the sentence about the **default** standard of gcc? I'm very sure OP did not even know the book is ancient and contains code which is legacy - at best. It **might** have been the best book (I dobt, but as it was the only C book I read, I'm not sure which other books of that time have been better) for Unix system programming, but that is certainly not true anymore. C has evolved since K&R - thankfully. – too honest for this site Sep 22 '15 at 12:48
  • @Jens Example code which demonstrates to newbies how they should use invalid forms of main(), and on top of that cause undefined behavior is "the best you can find"? – Lundin Sep 22 '15 at 13:03
  • so anyone suggest any book please , and i dont know what is c99 , c95 ,c ..... mean =))) . – thomas Sep 22 '15 at 13:05
  • @thomas All I can say is that there is a [book recommendation list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) which you shouldn't read, because it is full of crappy books... – Lundin Sep 22 '15 at 14:35
  • @Lundin "If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined." This is not the same as undefined behaviour of the program. If you don't care about the termination status, you don't have to write a return statement in `main`. – n. m. could be an AI Aug 15 '17 at 05:42
  • @n.m. It is not for the programmer to decide. If the host environment uses the return value of a program (Linux for example), then omitting the return value or writing an empty `return ;` invokes undefined behavior as per C90 5.1.2.2.3. Note that in these comments I was talking about K&R's failure to understand the "new" 1990 C standard. In latter C standards you may omit the return value of main(). – Lundin Aug 15 '17 at 06:15
  • @Lundin I'm too cheap to buy an actual copy of an outdated standard and there's no section 5 in the draft I have, so I have no idea what you are talking about. If you mean 2.1.2.2 then that's exactly the paragraph I have quoted for you, read it again. – n. m. could be an AI Aug 15 '17 at 09:28
  • @Lundin as for your allegations that K&R "failed to understand" C90, the second edition went out in 1998 when C89/C90 was not avalilable in its final form. So please stop using emtiomally charged phrases like "rookie mistake". Dennis Ritchie was an influential member of X3J11 when it developed ANSI C; were you? – n. m. could be an AI Aug 15 '17 at 09:49

2 Answers2

1
int main( void ) 
{
     int a ;
     for (a = 0; getchar() != EOF ; ++a);
     printf ("%d\n",a);  
     return 0;
}

On Unix platform run this code and when you want to exit introduce EOF by ctrl+d, if you are on windows then EOF is introduced by ctrl+z

So basically when you exit you will get the count of number of times your loop ran.

If you want to print out each input then you need to get rid of the ; at the end of for loop

int main( void ) 
{
     int a ;
     for (a = 0; getchar() != EOF ; ++a)
     printf ("%d\n",a);  
     return 0;
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    @outofmind No. Implicit int has been removed from the C language. `main()` is no longer a valid form. [See this](http://stackoverflow.com/a/31263079/584518). – Lundin Sep 22 '15 at 12:52
  • @Lundin ok, you're right for C99 and newer - it was valid before, however, I've deleted my comment – outofmind Sep 22 '15 at 12:56
  • 1
    @outofmind But in C89 you would have to write a return statement, which K&R didn't. So the original K&R example invokes undefined behavior. From C99, as a special case, the main() function is allowed to omit the return statement, which is then equivalent to `return 0`. – Lundin Sep 22 '15 at 12:59
0

I strongly suspect that the console closes immedeately after the loop ends. Try to insert something like system("pause") to prevent the console from closing.

The loop loops until you type EOF and prints the number of characters typed so far. To type EOF, you have to hit ctrl-z and return (in a test I had to do this after a return, so return, then ctrl-z, then return). If the console closes directly after the ctrl-z, you can add this system("pause") to wait for another key afterwards, so you see the output.

outofmind
  • 1,430
  • 1
  • 20
  • 37