Explain This Basic Program To Beginner

This is a discussion on Explain This Basic Program To Beginner within the C Programming forums, part of the General Programming Boards category; Hi, I am working my way through a C Programming book but I am confused by this extract of code ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    Explain This Basic Program To Beginner

    Hi,

    I am working my way through a C Programming book but I am confused by this extract of code from book, which is suppose to locate lines ending with 'ould' and print them.

    Code:
    #include <stdio.h>												
       #define MAXLINE 1000 /* maximum input line length */					
       int getline(char line[], int max)   int strindex(char source[], char searchfor[]);					
       char pattern[] = "ould";   /* pattern to search for */					
       /* find all lines matching pattern */   main()					
       {       char line[MAXLINE];       int found = 0;					
           while (getline(line, MAXLINE) > 0)           if (strindex(line, pattern) >= 0) {					
                   printf("%s", line); found++;}
    					
           return found;   }					
       /* getline:  get line into s, return length */   int getline(char s[], int lim) {
    int c, i;
    					
           i = 0;       while (--lim > 0 && (c=getchar()) != EOF && c != '\n')					
               s[i++] = c;       if (c == '\n')				
    			
    							[IMG]file:///page63image24688[/IMG]			
    							[IMG]file:///page63image24960[/IMG]			
    							[IMG]file:///page63image25232[/IMG]			
    												s[i++] = c;
    				
    			
    		
    														s[i] = '\0';
    					return i;}
    					
    /* strindex:  return index of t in s, -1 if none */int strindex(char s[], char t[]) {
    int i, j, k;
    					
        for (i = 0; s[i] != '\0'; i++) {        for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)					
                ;        if (k > 0 && t[k] == '\0')				
    			
    												}
    				
    									return i;
    				
    			
    			
    return -1;} 
    What does the following:

    Code:
    #define MAXLINE 1000
    actually mean, is 'MAXLINE' a built in function of C or something which they have created?

    Then they use:

    Code:
    getline(char line [], int max)
    So 'getline' is fairly self explanatory, then they indicate the type or result they are expecting which is 'char' for character. Now is that being put into a array, I am wondering because of '[]'? Furthermore, what is the 'int max' for?

    Everything after that makes sense, they create a char array but why does it equal "ould", I know that the snipet that they are searching for.

    Code:
    char pattern[] = "ould";
    Now we create another array called line, which refers to that 'MAXLINE' from earlier?

    Code:
     main()						
       {       char line[MAXLINE];       int found = 0;			
           while (getline(line, MAXLINE) > 0)           if (strindex(line, pattern) >= 0) {			
                   printf("%s", line); found++;}
    			
           return found; 
       }
    Then we use a while loop, so while there is a line search for pattern and then print the string.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Grab any decent C language tutorial from the web and start reading.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Is there any you recommend, the one I got doesn't sem to be for begginers?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    C Book Recommendations <-- lots of good stuff here

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    I got the first book, it doesn't seem great for beginner.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    It's not great for a beginner. It's a dense, dry reference book, and quite out of date with the latest C standard (which itself is 12 years old). Try the "21 days" book, many here seem to like it as a total beginner's book.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    #define MAXLINE 1000
    During the preprocessing step (before the file is compiled) all instances of the word MAXLINE are replaced with 1000.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ASHWHIT View Post
    Is there any you recommend, the one I got doesn't sem to be for begginers?
    Go get a different one... Really...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    [IMG]file:///page63image24688[/IMG]
    Oh that's really helpful
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this to me (beginner w/ pointers)?
    By klawson88 in forum C Programming
    Replies: 7
    Last Post: 10-02-2010, 10:07 PM
  2. Replies: 19
    Last Post: 04-12-2009, 08:37 AM
  3. Basic compiling problem for a beginner
    By crazychile in forum C Programming
    Replies: 4
    Last Post: 09-21-2008, 02:27 AM
  4. can someone explain why this is? (very basic c++)
    By sway1 in forum C++ Programming
    Replies: 3
    Last Post: 07-08-2008, 01:24 AM
  5. very basic beginner problem
    By sandingman1 in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 04:48 PM

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