Thread: Program that shows biggest line...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    Program that shows biggest line...

    this program counts lines and prints out the longest line from input.
    Code:
    #include <stdio.h>
    
    #define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    int main()
    {
        int len;
        int max;
        char line[MAXLINE];
        char longest[MAXLINE];
        
        max = 0;
        while ((len = getline(line, MAXLINE)) > 0)
            if(len > max) {
                max = len;
                copy(longest, line);
               }
         if(max > 0)
            printf("%s", longest);
         return 0;
    }
    
    int getline(char s[], int lim)
    {
        int c, i;
    
        for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
            s[i] = c;
        if(c == '\n'){
            s[i] = c;
            ++i;
          }
        s[i] = '\0';
        return i;
    }
    
    void copy(char to[], char from[])
    {
        int i;
        
        i = 0;
        while((to[i] = from[i]) != '\0')
            ++i;
    }
    ok im reading the c programming language 2nd edition ever since i started, everything from the beginning until now i was able to understand.
    i dont get alot of things in this program so i will be asking tons of questions about this...

    1. the book doesnt explain '\0' very clear about how it works in this program, only to what it does... can someone explain to me the purpose of it here?

    2.The "if" statement in getline has the same statement body as the for loop so what was the point of that??

    3. How does copy function in main if it doesnt return anything??

    4. in the for loop in getline, why is it"i < lim -1" and not i<lim? doesnt lim already represents max?

    sorry and can u please give me a simple explanation of the entire program too?... those questions were things i dont understand but a clear explanation of the entire program would help me even further. thanks...
    Last edited by i_can_do_this; 07-19-2006 at 08:45 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by i_can_do_this
    1. the book doesnt explain '\0' very clear about how it works in this program, only to what it does... can someone explain to me the purpose of it here?

    2.The "if" statement in getline has the same statement body as the for loop so what was the point of that??

    3. How does copy function in main if it doesnt return anything??

    4. in the for loop in getline, why is it"i < lim -1" and not i<lim? doesnt lim already represents max?

    sorry and can u please give me a simple explanation of the entire program too?... those questions were things i dont understand but a clear explanation of the entire program would help me even further. thanks...
    Assuming you have the second edition.
    1. section 5.5 page 104
    4. refer to question 1
    2. it is explained in the page before.
    3. you miss the concept of a pointer refer to the beginning of the chapter.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    [EDIT]
    Beaten by Maragato, but if the book does not explain well enough, perhaps the
    following will.
    [/EDIT]

    Ok here goes:

    1. In C, a string is a concept, and while it is physically just an array of
    characters, in order to use special string processing functions such as strcmp
    (used for comparing strings for equality), there needs to be some standard way
    to determine where the string ends (since the string is not necessarily filling
    the array it is stored in). Thats why we have \0' - its just a standard way of
    saying "this is the end of the string - anything after this does not matter".
    That's why the program is using '\0' - to make them standard C strings.

    2. I assume you mean this:

    Code:
    if(c == '\n'){
    s[i] = c;
    ++i;
    }
    well if you read the code carefully, you will see that the for loop is only executing
    one statement, and the for loop will end if a user presses enter ('\n'), or EOF.
    In the event that the user pressed enter to break the loop, the if statement
    entered above will cause the '\n' to be stored in the string, which would not
    happen with the current structure of the for loop. We would not want to store
    the EOF in the string since we want a string of 0 length. Basically, this statement
    is designed to cover the possibility that the user enters less than the specified
    number of characters, to ensure that the '\n' is also stored - its not necessary,
    it's just a design choice. The function fgets exhibits the same behavior.

    3. Arrays are always passed by reference, something you may not have covered
    yet if you're asking. Look up pointers. Basically, when passing an array to a
    function, any modiications made to the array inside the function modify the
    array originally passed (different to normally passing an int to the function,
    where the function receives a copy of the data, not the original data).

    4. Since we need to have the '\0' character to make it a string, we need to leave
    space. Lets say that lim is 10. That means our array char hold 10 characters,
    (from array indices 0 to 9). But if we need to always have a '\0', then we are
    always using one character - so we can really only read in a maximum of 9
    data characters (array indices 0 to 8), then a '\0'.


    [ALSO]

    If you understand those answers, you pretty much have the program itself
    down, so I'll just summarise what it does:

    1. Program enters a loop.
    2. User is prompted to enter some text.
    3. If the text entered is longer than the last longest value saved in the
    longest array, overwrite it using the copy function, otherwise don't.
    4. If the user enters no characters (By signalling EOF to the program), end the
    loop.
    5. Print out the string stored in the longest array.

    [/ALSO]
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Arrays are always passed by reference pointer ...

    We don't want to breed confusion about references and how they aren't really in C.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by citizen
    > Arrays are always passed by reference pointer ...

    We don't want to breed confusion about references and how they aren't really in C.
    IS this accurate?
    http://www.c-faq.com/ptrs/passbyref.html

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yes. I've never seen anything inaccurate on c-faq.com, and that particular entry is no exception.
    If you understand what you're doing, you're not learning anything.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Sorry if my statement caused confusion, I suppose not everyone knows that the
    term "pass-by-reference" with regard to C means pointers are involved. I have
    never had confusion about that terminology when used in C but I understand
    how it could be misleading since the most accurate term is pass-by-pointer.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How use a Web Interface with a C command line program
    By vmehta211 in forum C Programming
    Replies: 3
    Last Post: 01-18-2006, 12:19 PM
  2. makefile
    By twans in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2005, 12:16 AM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM