Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-14-2008, 06:27 AM   #1
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Smile Program : Not getting the desired output


For the following code i am getting double the actual result. For example if i enter 2 characters i get 4 as the output.

#include <stdio.h>

int main ()
double nc = 0; /* count of entered characters */

while (getchar()!=EOF)
printf ("The number of entered characters = %.0f",nc);
return 0;

Note: This is how i enter the input

when i execute the output file, because of getchar() it waits for input. I enter some character and press enter. Continues this until i press Ctrl-D (EOF in Unix).

Old 11-14-2008, 06:42 AM   #2
Senior Member
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Blog Entries: 1

Rep: Reputation: 83
Just out of curiousity, what happens, exactly, when you enter 3 characters?
Old 11-14-2008, 07:17 AM   #3
Registered: Sep 2008
Location: Calcutta,India
Distribution: Fedora 11, OpenSUSE 11.0
Posts: 116

Rep: Reputation: 17

Originally Posted by thelink123 View Post

printf ("The number of entered characters = %.0f",nc);
i thinsk this would work

printf("The number of entered characters = %lf",nc);

as datatype of nc is double
it should not be called with float syntax(%f)
instead use %lf
Old 11-14-2008, 09:46 AM   #4
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,125
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I think you are typing incorrectly, perhaps hitting return or some such. Running your program typing ab followed by enough ctrl-d to get eof to work gives me 2 as the output.
Old 11-17-2008, 01:59 AM   #5
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Smile Its Working

It is working now...The problem was with the incorrect input entry.
Thanks estabroo


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass the output of one program to another program without backticks? BrianK Linux - General 4 11-11-2008 06:27 PM
how to get the desired output from the following code: pdklinux79 Linux - Newbie 1 06-10-2008 01:51 PM
How to find package name for desired program or command? ozegoods Linux - Software 1 04-17-2007 02:16 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
getting output from a program executed from c++ dfuse Programming 6 08-22-2003 02:24 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration