0

I'm trying to find out the number of tabs, spaces and newlines in my C program. The code is :

#include <stdio.h>

void main()
{
    char c;
    int tabs=0 , spaces=0 , nl=0 ;
    printf("Provide the input");
    scanf("%c" , &c);
    while(c != EOF)
    {
        if(c == ' ')
        spaces++;
        else if(c == '\n')
        nl++;
        else if(c == '\t')
        tabs++;
        scanf("%c" , &c);
    }
    printf("blanks:%d\nspaces:%d\nnewlines:%d" , spaces , tabs , nl);
}

Well, I'm expecting it to do just that but the code is just not moving beyond accepting the input phase. What am I doing wrong? Below is what my command line looks like:

[tejas@localhost The_C_Programming_Language]$ cc Exercise_1-8.c

[tejas@localhost The_C_Programming_Language]$ ./a.out

Provide the input seguiofgawie

gweuigwh

e u w   f qw[uwf            
[PHWUO FEFF 
qah fuwpfyh fweor

(I keep pressing the Return key but to no end. Please help. Thank you for reading. This is my first question, hope i didn't do anything wrong...)

EDIT: getchar() doesn't work either, and I'm currently using Fedora 26 and GEDIT as my text editor

EDIT: getchar() works, in order to send the EOF character, one should press Ctrl+D on an empty line and if you want it done in with just one press of the RETURN key, change your loop condition to variable != '\n'. I apologize for spreading misinformation.

19
  • EOF is not equal to Return. On most systems it is Ctrl + Z. But it still depends. Commented Dec 4, 2017 at 12:04
  • 2
    The return key == '\n' (on unix based OS'es), on Windows it's \r\n Commented Dec 4, 2017 at 12:05
  • 3
    (1) scanf("%c" , &c); never sets c to EOF. (2) EOF is not a valid value of type char. Please see fgetc and getchar, and pay close attention to their return type. (3) Identify the source that taught you to write void main() and never trust that person, book or web site again. Commented Dec 4, 2017 at 12:08
  • If you need to read the single key clicks, you have to set the terminal as RAW and then you may use fgetc or read and not scanf. Commented Dec 4, 2017 at 12:13
  • 1
    stackoverflow.com/questions/1798511/… Commented Dec 4, 2017 at 12:15

2 Answers 2

2

There is a small error here that you are commiting, by comparing the scanned char value to EOF. Quoting another answer

EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.

However, since you are reading from stdin, you won't be able to enter it as a single char since it will be considered as 2 characters and will not produce the desired result. In a nutshell, your program is behaving in the desired way. In order for it to exit input mode, just change your while condition to something which you are able to meet.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This worked in Windows IIRC. One last thing: How can I write code in comments? Like the 3rd comment on my question?
0

i think, a character given by the user cannot be compared to EOF(end of file) as you are not dealing with any file i.e., you have not opened any file in order to verify its end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.