I am a B.Tech 1st year student.I am unable to understand the program given below.Please any one explain me.Any help would be appreciated.

This program is for printing longest line of given input lines..
[#include <stdio.h>

#define MAXLINE 1000 /* maximum input line length */

int getlines(char line[], int maxline);

void copy(char to[], char from[]);

/* print the longest input line */

main()

{

int len; /* current line length */

int max; /* maximum length seen so far */

char line[MAXLINE]; /* current input line */

char longest[MAXLINE]; /* longest line saved here */

max = 0;

while ((len = getlines(line, MAXLINE)) > 0)

if (len > max) {

max = len;

copy(longest, line);

}

if (max > 0) /* there was a line */

printf("%s", longest);


return 0;

}

/* getlines: read a line into s, return length */

int getlines(char s[],int lim)

{

int c, i;

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)

s = c;

if (c == '\n') {

s = c;

++i;

}

s = '\0';

return i;

}

/* copy: copy 'from' into 'to'; assume to is big enough */

void copy(char to[], char from[])

{

int i;

i = 0;

while ((to = from) != '\0')

++i;

}]

It is much easier to understand the program if you use CODE tags around your source code: It makes your code much easier to understand, gives syntax highlighting, protects indentation, gives line numbers and is a generally great idea.

The way to understand a program is to break it into smaller pieces, understand each piece, then understand how the pieces work together. In this case, understand the getlines function first, then the copy function, then main to see how it works together. Or start with main and go look at the two other functions when you come to them. When you have spent some time thinking, you will be ready to ask a more focused question, and prepared to understand the answer.

By the way, you must declare main as one of int main(void) or int main(int argc char** argv) (or equivalent). Yes, C does quietly declare a default int return type for functions that don't specify anything, but it is neither clever nor wise to not say what you mean when you are writing code.

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getlines(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main()
{
   int len; /* current line length */
   int max; /* maximum length seen so far */
   char line[MAXLINE]; /* current input line */
   char longest[MAXLINE]; /* longest line saved here */
   max = 0;
   while ((len = getlines(line, MAXLINE)) > 0)
   if (len > max) {
      max = len;
      copy(longest, line);
   }
   if (max > 0) /* there was a line */
   printf("%s", longest);
   return 0;
}
/* getlines: read a line into s, return length */
int getlines(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;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
   int i;
   i = 0;
   while ((to[i] = from[i]) != '\0')
   ++i;
}

On first glance the code looks quite sloppy and there are some errors in it.

I just compiled it to confirm and yep... it doesn't even work properly.

Mistakes / errors:
o main should be int main(void) or int main(arg1,arg2)
o Getlines function will never return 0, which means the while loop will not end *unless* the for loop in getlines doesn't even execute at all (Which never happens)
o The length returned does not seem to match the actual length of the line. (The numbers are consistent and will still work but they are too big, unless you want to include extra characters like '\n' and '\0')

There's also some redundant things I think that can be shortened but that isn't very important.

But here's what it's supposed to do:
Main creates a few variables to use, and initializes max to 0.
Then it calls the getlines() command in the while loop.

The getlines command has a for loop that let's you input characters with the keyboard (getchar). Then it puts these characters in an array.
When you press enter, it stops the for loop and increments i by 1 to add the string terminator '\0' Then it returns i (which here, is the length of the complete line including the newline / terminator characters which is a bit weird, but it still works out.)

Then main checks if variable len (which now holds the value of i, the length of the line) is bigger than max (which is 0 by default).

If this is the case, max gets the value of len, and copy is called.
Copy takes the contents of line[], and puts them in longest[] with the while loop.

When that's done, while runs again and asks for another line, if the next line is smaller than the biggest line nothing will happen and while repeats again, until you create a line with size 0 or less.

If the while loop encounters a line of length 0, it will check if the maximum length exists (ie.: is not 0).

If this is the case it will print the longest line it encountered (which it copied with the copy function)

After that it returns 0; (EXIT_SUCCESS)

There is a "copy" function and a "getlines" function at the top of program.Where they are actually constructed.Actually they are defined at top,where they are constructed or given instructions.Please help me.

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getlines(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main()
{
   int len; /* current line length */
   int max; /* maximum length seen so far */
   char line[MAXLINE]; /* current input line */
   char longest[MAXLINE]; /* longest line saved here */
   max = 0;
   while ((len = getlines(line, MAXLINE)) > 0)
   if (len > max) {
      max = len;
      copy(longest, line);
   }
   if (max > 0) /* there was a line */
   printf("%s", longest);
   return 0;
}
/* getlines: read a line into s, return length */
int getlines(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;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
   int i;
   i = 0;
   while ((to[i] = from[i]) != '\0')
   ++i;
}

On first glance the code looks quite sloppy and there are some errors in it.

I just compiled it to confirm and yep... it doesn't even work properly.

Mistakes / errors:
o main should be int main(void) or int main(arg1,arg2)
o Getlines function will never return 0, which means the while loop will not end *unless* the for loop in getlines doesn't even execute at all (Which never happens)
o The length returned does not seem to match the actual length of the line. (The numbers are consistent and will still work but they are too big, unless you want to include extra characters like '\n' and '\0')

There's also some redundant things I think that can be shortened but that isn't very important.

But here's what it's supposed to do:
Main creates a few variables to use, and initializes max to 0.
Then it calls the getlines() command in the while loop.

The getlines command has a for loop that let's you input characters with the keyboard (getchar). Then it puts these characters in an array.
When you press enter, it stops the for loop and increments i by 1 to add the string terminator '\0' Then it returns i (which here, is the length of the complete line including the newline / terminator characters which is a bit weird, but it still works out.)

Then main checks if variable len (which now holds the value of i, the length of the line) is bigger than max (which is 0 by default).

If this is the case, max gets the value of len, and copy is called.
Copy takes the contents of line[], and puts them in longest[] with the while loop.

When that's done, while runs again and asks for another line, if the next line is smaller than the biggest line nothing will happen and while repeats again, until you create a line with size 0 or less.

If the while loop encounters a line of length 0, it will check if the maximum length exists (ie.: is not 0).

If this is the case it will print the longest line it encountered (which it copied with the copy function)

After that it returns 0; (EXIT_SUCCESS)

There is a "copy" function and a "getlines" function at the top of program.Where they are actually constructed.Actually they are defined at top,where they are constructed or given instructions.Please help me.

There is a "copy" function and a "getlines" function at the top of program.Where they are actually constructed.Actually they are defined at top,where they are constructed or given instructions.Please help me.

What is your exact question, do you want to see where they are constructed?

the beginning of the code where it says:

int getlines(char line[], int maxline);
void copy(char to[], char from[]);

These are the prototypes, they are like declaring a variable, you say what type of function it is, but you don't say what the contents are. (With these you say what the 'return type' of a function is, and the 'input' (parameters))

When you see:

int getlines(char line[], int maxline) {
/*Some code */
}

void copy(char to[], char from[]) {
/*Some code */
}

That's where they are ''defined''. (Or given instructions to execute).
To actually make them execute the given instructions, the 'main' function has to 'activate' them

What is your exact question, do you want to see where they are constructed?

the beginning of the code where it says:

int getlines(char line[], int maxline);
void copy(char to[], char from[]);

These are the prototypes, they are like declaring a variable, you say what type of function it is, but you don't say what the contents are:

When you see:

int getlines(char line[], int maxline) {
/*Some code */
}

void copy(char to[], char from[]) {
/*Some code */
}

That's where they are ''defined''.

I understood where they are defined but I'm not getting where are instructions of that
function.Every function has its work I guess.We generally give the instructions below the

function(arg1,arg2)

For example how copy function is working.Where are the instructions of that function in program.That's not immediately below that function in given program.This is what I want to know.Where are the instructions of that function not declaration.

It's here, in the original file in my first post it should be at roughly line 22 and 36:

/* getlines: read a line into s, return length */
int getlines(char s[],int lim) /* GETLINE FUNCTION INSTRUCTIONS */
{
   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;
} /* END OF GETLINE FUNCTION INSTRUCTIONS */
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[]) /* COPY FUNCTION INSTRUCTIONS */
{
   int i;
   i = 0;
   while ((to[i] = from[i]) != '\0')
   ++i;
} /* END OF COPY FUNCTION INSTRUCTIONS */

It's here, in the original file in my first post it should be at roughly line 22 and 36:

/* getlines: read a line into s, return length */
int getlines(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;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
   int i;
   i = 0;
   while ((to[i] = from[i]) != '\0')
   ++i;
}

Ok!In definition,we have given

int getline(char line[], int maxline);

Here we have given

int getlines(char s[],int lim)

.Can we do that.
Thanks for your patience..

Yes, if you type in one .c file:

int getline(char line[], int maxline);

int main(void)
{
   char array[];
   int something;
   getline(array,something);
}

int getline(char bob[], int abc)
{
   /* Do stuff */
}

It will still work. the part with and without the instructions dont need the same name in parameters. (Even though it looks a bit messy)

The only requirement is that they both use same TYPES (eg.: int / char / char array)

Thanks for your precious help.I now get some clarity.I have a doubt since I started learning C language about return.What is the use of return.Some programs run with not respect to it.Even if we delete return statement,program will run safely.Then what is the use of return in programming languages especially in C.

Yes, if you type in one .c file:

int getline(char line[], int maxline);

int main(void)
{
   char array[];
   int something;
   getline(array,something);
}

int getline(char bob[], int abc)
{
   /* Do stuff */
}

It will still work. the part with and without the instructions dont need the same name in parameters. (Even though it looks a bit messy)

The only requirement is that they both use same TYPES (eg.: int / char / char array)

Thanks for your precious help.I now get some clarity.I have a doubt since I started learning C language about return.What is the use of return.Some programs run with not respect to it.Even if we delete return statement,program will run safely.Then what is the use of return in programming languages especially in C.

Most compiler's have a default return type of int - like Borland used to.

Today, you should be explicit about your return types, and they should be included in your function prototype.

What is the return good for?

Any answer to a computation the function is doing! Serious programming doesn't use much in the way of global scoped variables - they're local 99% of the time. A return is ONE way (a pointer to the variable being passed as a parameter to the function is another way), to bring the answer back to the calling function.

Thanks for your help..I want to know the use of return

There's multiple uses for return:
If I write

int main(void) {
   /* Do stuff */
   return 0;
}

If I open the program.exe in windows, it will do stuff and then ''exit'' and give windows the number 0. This makes windows know that the program quit successfully (because 0 means '' QUIT OK! '')
If it sends a 1 it would mean ''ERROR! PROGRAM QUIT''. a 2 could be ''not enough memory!'' etc. etc.

For more information about return for main() and errors:
MSDN Main() Return Values

An other use for return is to give 'information' back to a different function.
If you type:

int main(void) {
int a;
a = give_me_ten();
printf("This program gave me the number %d", a);
}

int give_me_ten() {
int bob = 10;
return bob;
}

It would make variable 'a' have the value 10.
In this case it's not very useful, but if you declare variables in a function it can only be used in *that* function.
If I typed:

int main(void) {
   give_me_ten();
   printf("This program gave me the number %d", bob); /* ERROR: Bob only exists in ''give_me_ten()'' */
}

int give_me_ten() {
   int bob = 10;
   return bob;
}

It would say huh? What is bob? I don't know bob! Because bob only exists in give_me_ten()

There's a lot of other reasons to use them but a few ones are:
o To diagnose errors / exitcodes
o To transfer information
o Conditional structures (if / while / for) (return 1 = true, return 0 = false)
o With structures when you want to return a 'copy' of information that a function created.

Some times,the program doesn't get wrong even without a return value.But if we give return 0;,some times program will get bad why?..

There's multiple uses for return:
If I write

int main(void) {
   /* Do stuff */
   return 0;
}

If I open the program.exe in windows, it will do stuff and then ''exit'' and give windows the number 0. This makes windows know that the program quit successfully (because 0 means '' QUIT OK! '')
If it sends a 1 it would mean ''ERROR! PROGRAM QUIT''. a 2 could be ''not enough memory!'' etc. etc.

For more information about return for main() and errors:
MSDN Main() Return Values

An other use for return is to give 'information' back to a different function.
If you type:

int main(void) {
int a;
a = give_me_ten();
printf("This program gave me the number %d", a);
}

int give_me_ten() {
int bob = 10;
return bob;
}

It would make variable 'a' have the value 10.
In this case it's not very useful, but if you declare variables in a function it can only be used in *that* function.
If I typed:

int main(void) {
   give_me_ten();
   printf("This program gave me the number %d", bob); /* ERROR: Bob only exists in ''give_me_ten()'' */
}

int give_me_ten() {
   int bob = 10;
   return bob;
}

It would say huh? What is bob? I don't know bob! Because bob only exists in give_me_ten()

There's a lot of other reasons to use them but a few ones are:
o To diagnose errors / exitcodes
o To transfer information
o Conditional structures (if / while / for) (return 1 = true, return 0 = false)
o With structures when you want to return a 'copy' of information that a function created.

What happens if If we give return 0; to a malicious program.If a program has errors and if we give return 0;.What will compiler return 0 saying that no errors forcidly or return 1 saying it has errors?..
Sorry for bad English..

No no the compiler doesn't care if you write return 0; or return 1; in the program.
It is when you run the program, and errors happen *IN* the program you can use it to tell windows/other program that something goes wrong *inside* your program.

Like if you make a program that creates file, and if the file already exists it gives an error (because 2 files can't have the same name):

int main(filename) {
   if(filename already exists) {
      return 1; /* This tells the other program / windows something went wrong */
   } else {
      create_file(filename);
      return 0; /* This tells the other program / windows that all went ok */
   }
}

Malicious programs don't work like that, they have nothing to do with return 0; :P

Hi challarao, if you want explanation for this program. Please contacr me.

Hi challarao, if you want explanation for this program. Please contacr me.

Hey I have just planned to ask you it in dorm and wrote whole program in a notebook...
what a coincidence it is...

Oh it is already exceeded 2 pages ..I am now closing the thread as solved
Thanks for everyone's help expecially [Alpha]-Omega's..Thankyou Alpha

Oh it is already exceeded 2 pages ..I am now closing the thread as solved
Thanks for everyone's help especially [Alpha]-Omega's..Thank you Alpha

challarao, as continuation yesterday if we enter a then after i becomes 1, them if we enter b then i becomes 2,it will continue until before we enter '\n'.Then '\n' also counted as a character for that line. So the getline function returns the value of length of the line including new line.So yesterday's confusion was cleared.The purpose of including new line as a character is to get the normal shell command in the newline , that's all.

challarao, as continuation yesterday if we enter a then after i becomes 1, them if we enter b then i becomes 2,it will continue until before we enter '\n'.Then '\n' also counted as a character for that line. So the getline function returns the value of length of the line including new line.So yesterday's confusion was cleared.The purpose of including new line as a character is to get the normal shell command in the newline , that's all.

Do you mean that student@student-laptop is to come below our out put ...keeping mind that newline is not our input virtually
Thanks...

Mutti gadiki twitter lo sarigga pilavadam neirchukoomani cheppu..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.