Thread: k&r chapter 1 question

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63

    k&r chapter 1 question

    quick question, I am on my 4th book and i am compiling a few examples out of Chapter 1 but not getting correct results. The program is not counting lines. For example:

    Code:
    #include <stdio.h>
    
    
    main()
    {
        int c, n1;
        
        n1 = 0;
        while (( c = getchar()) != EOF)
            if ( c == '\n')
                ++n1;
        printf("%d\n", n1);
    }
    and also,...

    Code:
    #include <stdio.h>
    
    
    /* count charcters in input; 1st version */
    
    
    main()
    {
        long nc;
        
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    }
    I am compiling them using CLANG 6.0.0 on freebsd 11.2. They compile however am not giving me total char count...just another empty blank line. THANKS!

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Besides your rather ancient style (leaving "int" off of main and not initializing variables when declaring them) I don't see anything wrong.

    How exactly are you running the programs?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    What are you entering into the program?

    How are you signalling EOF?

    This is what I entered:
    test
    it
    big
    time.

    And this is the result after signalling EOF:
    18

    This is from the code in the second snippet.

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63
    well a quick google and I used "CTRL + D" seems to "signal EOF" now

  5. #5
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Quote Originally Posted by _jamie View Post
    quick question, I am on my 4th book and i am compiling a few examples out of Chapter 1 but not getting correct results. The program is not counting lines. For example:

    [code]


    Code:
    #include <stdio.h>
    
    
    /* count charcters in input; 1st version */
    
    
    main()
    {
        long nc;
        
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%ld\n", nc);
    }
    I am compiling them using CLANG 6.0.0 on freebsd 11.2. They compile however am not giving me total char count...just another empty blank line. THANKS!
    The program has no an exit condition (only Strg-C). But the result is correct:
    1 + Wolf = 5
    1 + Ente = 5
    0 = 1
    Result = 11
    Code:
    #include <stdio.h>
    #include <stdlib.h>
      
    /* count charcters in input; 1st version */
     int main(void)
    {
      int c, char_number = 0; 
        
        do
        {
            c = getchar();
            char_number++;
        }while(c != '0');  //"0" for end of input
          
      printf("\nThat are in total %3d characters.\n", char_number);
        return(0);
    }

  6. #6
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63
    this code was right out of the book, and never mentioned 'CTRL+D" to exit. Thanks Kernelpanic!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chapter 5- Question 7
    By fetahi3212 in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2016, 04:06 AM
  2. Jumping in C++ Chapter 5 Question 2
    By etricity in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2014, 02:18 AM
  3. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  4. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM
  5. K&R Chapter 1 - basic question
    By LienolCrazel in forum C Programming
    Replies: 1
    Last Post: 05-05-2013, 08:52 AM

Tags for this Thread