K&R variable confusion

This is a discussion on K&R variable confusion within the C Programming forums, part of the General Programming Boards category; This is a question for those familiar with K&R and for those who don't need to be. On page 29 ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    K&R variable confusion

    This is a question for those familiar with K&R and for those who don't need to be. On page 29 second edition you can find the following function as part of a program. I don't understand how the value of "lim" is decided.
    Code:
    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] = c;
    	return i;
    }
    I believe this function will return the length of the program but I can't figure out how if it doesn't know what lim is.

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    MAXLINE is the second argument of the function getline.
    It is a symbolic constant defined as 1000.
    So 'lim' would have 1000.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    lim is the size of the s[] array. It's how many char can fit into s, minus the string terminator, which takes up one element. The function is similar to the library function fgets().

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for your replies.

    @stevesmithx - The program does define MAXLINE as 1000

    Code:
    #define MAXLINE 1000
    but the function getline uses lowercase so I guess it is not the same

    Code:
    int getline(char line[], int maxline);
    Of course I could be wrong.

    @swoopy - How does the program determine the value of lim? I see it is declared as an int but that is all.

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    Sorry,I misinterpreted your Post.
    but the function getline uses lowercase so I guess it is not the same
    Code:
    int getline(char line[], int maxline);
    It's just a function prototype.
    It doesn't really matter.
    Any variable name is given in a prototype.
    It can even be ignored like this:
    Code:
    int getline(char * , int );
    As swoopy said,lim is the size of the array.

    It is used for safety reasons in case the line is more than 1000 chars long.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by stevesmithx View Post
    It is used for safety reasons in case the line is more than 1000 chars long.
    Are you saying that the value of lim is 1000? And of so where is the connection in the program from the number 1000 to the variable lim?

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    Are you saying that the value of lim is 1000? And of so where is the connection in the program from the number 1000 to the variable lim?
    Yes.
    See the calling function and called function.
    Code:
    len = getline(line, MAXLINE)
    and
    Code:
    int getline(char s[],int lim){
    ...
    }
    Clear explanation is given in the K&R book.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Yes, I see it now. Thanks a lot.

    Unfortunately I don't feel the explanations are that clear but hopefully it will get clearer as time goes on. Thanks for your patience.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by stevesmithx View Post
    It can even be ignored like this:
    Code:
    int getline(char * , int );
    Might I add that that's considered bad practice? Don't remove the argument names from the prototypes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

Tags for this Thread


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