6

I am trying to learn the basics of C using The C Programming Language - Brian Kernighan and Dennis Ritchie

In the program below, I don't understand where the value for 'maxlineLength' comes from?

The for loop is set to run while 'i' is smaller than maxLineLength-1, but what is the value of maxLineLength and where does it come from?

From what I understand, when parameters are declared in a function like this a value is being passed to them, so they must surely be declared somewhere else to have the value passed in?

#include <stdio.h>
#define MAXIMUMLINELENGTH 1000
#define LONGLINE 20

main() {
 int stringLength;
 char line[MAXIMUMLINELENGTH];

  while((stringLength = getLineLength(line, MAXIMUMLINELENGTH)) > 0)
    if(stringLength < LONGLINE){
  printf("The line was under the minimum length\n");
    }
    else if (stringLength > LONGLINE){
        printf("%s", line);
    }
  return 0;
}


int getLineLength(char line[], int maxLineLength){
  int i, c;

  for(i = 0; i< maxLineLength-1 && ((c = getchar())!= EOF) && c != '\n'; i++)
    line[i] = c;

  if(c == '\n') {
      line[i] = c;
      i++;
  }

  line[i] = '\0';
  return i;
}
Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Michael King
  • 415
  • 6
  • 14
  • 1
    defined as macro `#define MAXIMUMLINELENGTH 1000` passes to function `getLineLength()` in while – Grijesh Chauhan May 19 '13 at 10:19
  • Hi Grijesh are MAXIMUMLINELENGTH and maxLineLength two completely different variables? – Michael King May 19 '13 at 10:21
  • `MAXIMUMLINELENGTH` is constant that expands to value `1000`at compilation time, Yes `maxLineLength` is different variable local to function `getLineLength()` , get an idea over [macro const from here](http://stackoverflow.com/questions/3216752/what-is-the-difference-between-macro-constants-and-constant-variables-in-c) – Grijesh Chauhan May 19 '13 at 10:23
  • But if I change maxLineLength to LengthLineMax so it bears no relation to MAXLINELENGTH what so ever the program still runs correctly so i still dont understand where the value comes from? – Michael King May 19 '13 at 10:27
  • When you call `getLineLength(line, MAXIMUMLINELENGTH)`, `MAXIMUMLINELENGTH` copy to function variable, you can use any name of variable `maxLineLength` or `LineLength` it doesn't matter its value passed at function call – Grijesh Chauhan May 19 '13 at 10:29
  • your welcome, you should also read 5 answer posted below all are good!, And accept at-lest one by click on right – Grijesh Chauhan May 19 '13 at 10:31

5 Answers5

5

From what I understand, when parameters are declared in a function like this a value is being passed to them, so they must surely be declared somewhere else to have the value passed in?

In this case, the function is being declared and defined at the same time. Pre-ANSI C allowed that. This is considered a bad practice today. You should add a forward declaration

int getLineLength(char line[], int maxLineLength);

above main in order to avoid declaring the function implicitly.

When this forward declaration is missing, the first use of the function becomes its implicit declaration. The compiler takes types of the expression parameters that you pass, and assumes that the function takes corresponding types as its parameters. It also assumes that the function returns an int. In this case the compiler has all the information necessary to figure out the correct signature, so the function is defined correctly. However, in more complex cases (say, if maxLineLength was declared as long long, not int) it would lead to undefined behavior, so you should always include a forward declaration of your functions.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3
getLineLength(line, MAXIMUMLINELENGTH)

MAXLINELENGTH is defined constant

Inside function getLineLength is this value "mapped" to maxLineLength variable

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
3

maxLineLength is a parameter/argument for the getLineLength function. You are expected to pass in a max length for the line when calling this function.

In the main function, it's done here:

getLineLength(line, MAXIMUMLINELENGTH)

He passes in MAXIMUMLINELENGTH as the max line length, which is equal to 1000 as can be seen by this definition in the code:

#define MAXIMUMLINELENGTH 1000

These #defines are specific to the preprocessor. It replaces all instances of MAXIMUMLINELENGTH with 1000 before compiling.

user123
  • 8,970
  • 2
  • 31
  • 52
2

The line

#define MAXIMUMLINELENGTH 1000

is the clue to the puzzle

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
2

C source files are pre-processed prior to compilation.

The #define MAXIMUMLINELENGTH 1000 is a C preprocessor directive that instructs the pre processor to replace all occurrences of the string MAXIMUMLINELENGTH with 1000.

If you use MAXIMUMLINELENGTH consistently through out this compilation unit (.C file) and decide at a later time that the line length should change you only have to make the change to pre-preprocessor directive and then that change is reflected through out this compilation unit.

So the line getLineLength(line, MAXIMUMLINELENGTH) is transformed by the preprocessor to getLineLength(line, 1000)

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51