what is wrong?

This is a discussion on what is wrong? within the C Programming forums, part of the General Programming Boards category; #include<stdio.h> main() { char c; int nc=1,nl=1; while((c = getchar())!=EOF) { if(c=='\n') nl++; else nc++; } printf("reached here!!!"); } Why ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    29

    what is wrong?

    #include<stdio.h>
    main() {
    char c;
    int nc=1,nl=1;
    while((c = getchar())!=EOF) {
    if(c=='\n')
    nl++;
    else
    nc++;
    }
    printf("reached here!!!");
    }


    Why doesn't the "reached here" get printed.
    I tried presssing Ctrl+c and Ctrl+z for EOF...
    C.B.Ashesh,
    Hyderabad, India

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Works for me using MSVC6 if I press ctrl-z <return> but is this standard behaviour?
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    I have tried replacing EFO with '\0' too, but still does' nt work... I am using a C compiler provided by Linux RH8.0
    C.B.Ashesh,
    Hyderabad, India

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char c;
    Vanilla char may not be capable of representing the value of EOF properly, you should be using int instead.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    19
    I have tried replacing EFO with '\0' too
    EOF != '\0' generally; it's usually -1. You should never use the absolute value, though.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    #include<stdio.h>
    main() {
    long nc;

    nc = 0;
    while(getchar()!=EOF)
    ++nc;
    printf("\n nc = %ld \n",nc);
    }


    The code also does not run in my system?

    The main doubt that arises is how I am supposed to represent EOF when I want to end the entries...

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    Finally the program ran when I used "Ctrl+D" to represent "EOF"

    Until now I was trying "Ctrl+Z" and "Ctrl+C"

    Since EOF has a value "-1"; so this mean that "Ctrl+d" too has "-1" as its ASCII value...

    how can I find the ascii value of Ctrl+C and Ctrl+Z? and what is their meaning?
    C.B.Ashesh,
    Hyderabad, India

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21