Thread: explain concept of eof in c prograamming

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    explain concept of eof in c prograamming

    i am clearing my concepts on programming and figured out this concept of eof using getchar()
    This program never prints the number of characters inputted in getchar .

    please help
    where n how it gets eof
    and why is it not printing tota number of counts

    Code:
    
    #include<stdio.h> main(){ int c ,count; while ((c=getchar())!=EOF) count++; printf("%d characters /n",count); }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First of all, review your post before pressing submit. All your code has been wrapped onto a single line.
    It's only saving grace is that the program is short enough to be still readable (just).

    > printf("%d characters /n",count);
    Try a \n instead of a /n
    \n is an actual newline.

    > while ((c=getchar())!=EOF) count++;
    What is the initial value of count?

    > where n how it gets eof
    If you're typing stuff in on your terminal, you normally press either ctrl-d (Unix/Linux) or ctrl-z (windows) at the start of a line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Quote Originally Posted by Salem View Post
    First of all, review your post before pressing submit. All your code has been wrapped onto a single line.
    It's only saving grace is that the program is short enough to be still readable (just).

    > printf("%d characters /n",count);
    Try a \n instead of a /n
    \n is an actual newline.

    > while ((c=getchar())!=EOF) count++;
    What is the initial value of count?

    > where n how it gets eof
    If you're typing stuff in on your terminal, you normally press either ctrl-d (Unix/Linux) or ctrl-z (windows) at the start of a line.







    ok i will surely keep your suggestion in mind
    And now please help me with this code
    Code:
    #include<stdio.h>
    main(){
    int c,count=0;
    while ((c = getchar()) != EOF){
    count++;
    }
    printf("%d\n",count);
    
    }
    why this code is not displaying the number of characters in count variable
    please explain this

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Your loop never ends because you never reach EOF.
    You are not reading from a file.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by batman4 View Post
    ok i will surely keep your suggestion in mind
    And now please help me with this code
    ...
    why this code is not displaying the number of characters in count variable
    please explain this
    Did you read the post by Salem? Look at the bottom part about signalling EOF from the keyboard.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is this going to turn into another one of those "why is my console window disappearing" questions?
    We have a FAQ for that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    how can we print something when our control comes to eof

    this is the code

    Code:
    #include<stdio.h>
    main(){
    int c,count=0;
    if((c=getchar())=!EOF)
    printf("process");
    
    printf("end");
    
    }
    In what case will it print end .
    if we press ctrl+d/ ctrl+c ,it should first print "end"
    Please explain me in which case my control will be in else condition.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by batman4 View Post
    this is the code

    In what case will it print end .
    if we press ctrl+d/ ctrl+c ,it should first print "end"
    Please explain me in which case my control will be in else condition.
    You don't have an else condition in that code. Also, not equals is written `!='. What you have won't work.

    Code:
    int main(void)
    {
    	int c;
    	if((c=getchar())!=EOF)
    		printf("processed character %c\n", c);
    	else
    		printf("end\n");
    	return 0;
    }
    If you press CTRL+C nothing will happen because the program will be terminated. Pressing CTRL+D (Linux, MacOS, etc.) or CTRL+Z (Windows) and enter will cause end to be printed.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you just typing in code for the fun of it just to see what we say?

    Code:
    $ gcc -Wall foo.c
    foo.c:2:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:4:17: error: lvalue required as left operand of assignment
    foo.c:3:7: warning: unused variable ‘count’ [-Wunused-variable]
    foo.c:3:5: warning: variable ‘c’ set but not used [-Wunused-but-set-variable]
    foo.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
    Your latest missive won't even compile, so the question "does it print...." is meaningless.

    > if((c=getchar())=!EOF)
    Notice you've written =! and not !=

    Also, if you want to be reasonably sure of seeing what printf prints, then always put a \n at the end of each line.

    > Please explain me in which case my control will be in else condition.
    What else - there is no else in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain me this concept!!
    By dala in forum C Programming
    Replies: 7
    Last Post: 09-27-2012, 06:21 AM
  2. Can someone explain this concept to me
    By sigur47 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2012, 07:35 AM
  3. help file concept
    By helloworld28 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 10:25 AM
  4. Concept
    By kusal in forum C Programming
    Replies: 12
    Last Post: 01-06-2007, 10:32 PM
  5. Concept help
    By Mithoric in forum Windows Programming
    Replies: 13
    Last Post: 04-18-2004, 03:05 PM