Thread: C program output doubt

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    C program output doubt

    Friends,

    I am doing K&R book problem 1.6 in exercise
    Code:
    #include <stdio.h>
    
    main()
    {
          int c;
          while(c=(getchar() != EOF))
           {
                printf("%d\n",c);
            }
                printf("on pressing ^d output is %d\n",c);
    }
    So C should get 0 or 1 depending on input is any character or EOF.

    On executing this program in GCC, I get the following output:
    > ./a.out
    d
    1
    1


    2
    1
    1

    On pressing ctrl d, I get
    0

    But I am not getting why 1 is getting printed Twice instead of only ONE time?

    Any answers?
    Thanks
    Saurav

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well you actually typed "d\n2\n^D\n" at different points in the execution. If you count the characters you typed it makes sense.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by whiteflags View Post
    Well you actually typed "d\n2\n^D\n" at different points in the execution. If you count the characters you typed it makes sense.
    So <ENTER> is taken as an input I assume?

    Thx
    Saurav

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by saurav_bhasin View Post
    So <ENTER> is taken as an input I assume?

    Thx
    Saurav
    Yes "ENTER" is taken as an input, which is nothing but '\n' character, so you're getting the first number for the "actual" character and the second for the "ENTER" you've pressed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by BEN10 View Post
    Yes "ENTER" is taken as an input, which is nothing but '\n' character, so you're getting the first number for the "actual" character and the second for the "ENTER" you've pressed.
    Thanks !!.

    Please do forgive my ignorance, I realized your solution soon after asking the question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  2. Replies: 5
    Last Post: 02-11-2008, 01:36 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  5. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM