This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Why printf() does not print?  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again and Happy New Year 2019!

I am learning C from the book "The C Programming Language" second edition by Brian Kernighan and Dennis Ritchie. There are a couple examples (character and line counting) that do not work for me when I code them. For example, line counting is:



Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?
 
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working.please check
 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working.please check Ideone
 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Meneses wrote:Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?


The code is going to stay ion the while loop until there is no more input - how are you terminating your input?  From the Linux command line, you would normally use Control-D:
You could also test by redirecting standard input from a file:
 
Adrian Meneses
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Adrian Meneses wrote:Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?


The code is going to stay ion the while loop until there is no more input - how are you terminating your input?  From the Linux command line, you would normally use Control-D:
You could also test by redirecting standard input from a file:


Thank you! I am not familiar with the EOF in the library and because of that I was terminating the program with Control-C.
 
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adrian

What environment are you running this in eg. Linux, Windows? How exactly are you running/testing it?
 
John Matthews
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Adrian

What environment are you running this in eg. Linux, Windows? How exactly are you running/testing it?


Apologies for the (sort of) duplicate post - when I started my reply no one else had posted, then I got distracted
 
Saloon Keeper
Posts: 20498
115
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linux (and Unix/MacOS) operate using signals. You can send a signal to a process with the "kill" command, which despite its name, doesn't actually kill processes, just targets the indicated process(es) for the designated signal. For example, the infamous "kill -9", which terminates a process immediately.

The CTRL-C code when issued in a command shell gets translated into a "kill -2" (SIGINT) that is sent to the currently-running application in that particular shell. The CTRL-D becomes a SIGQUIT, which the the stdin processor interprets as meaning "end of data". SIGINT by default kills a process, but since you can re-assign signal handling can also be used to do things like indicate a change in the environment so that the application would re-read its configuration files (as a common example).

SIGQUIT is preferable to SIGINT for terminating an application, because it allows the program logic to see end-of-file and do whatever post-data operations need to be done, whereas SIGINT kills the whole app without cleaning up (unless the app has configured a SIGINT handler routine).

If no application is running in a bash shell, SIGINT simply causes the shell to type "^C", but SIGQUIT causes the shell to terminate (logout).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!