• C

return value of main()

hi there, here is the code in a C book. when I compiled it, it gave me one warning: should return a value for the function. My question: does main() always have to return a value? and how to correct this error. This should be very easy for expert, I think.

// dem12-5.c
// This program asks the user to enter a string and then displays the string
// in reverse.

#include <stdio.h>

main()
{
  char line[81];
  char * ch_ptr = line;

  printf("\Enter a line of characters:\n");
  gets(line);

  //Find the end of the string

  while ( *ch_ptr != '\0' )
    ++ch_ptr;

  //ch_ptr now points to the null chr\aracter

  --ch_ptr;

  //ch_ptr now pints to the last character in the string

  printf("\nThe line in reverse is:\n\n");

  //The while loop displays all but the first character

  while (ch_ptr != line)
  {
    putchar(*ch_ptr);
    --ch_ptr;
  }

  //Display the first character

  putchar(*ch_ptr);
}


Chris
LVL 1
ny971181Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

timurCommented:
main() doesn't need to return a value.  You can define main as:

  int main()

or as

  void main()

if you use the latter, then you don't have to return anything.
It will return 0 for you automatically.

When you return a value, that value gets passed back to the
program that called your program.  For example, under DOS, if you
run program.exe from the command prompt (C:\> for example), then you can use the ERRORLEVEL batch variable to examine the result.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ny971181Author Commented:
The answer is that the declaration main () stands for int main (void) so if you have no return declaration for functions (also main is a function) C uses a int return.
0
rbrCommented:
In other words, when you declare a function without specific the
return type, such as "main()" instead of "int main()" or "void
main()", the compiler assumes that the return type is "int".  So
"main()" and "int main()" are identical.  If you don't like
that, you have to force the return type to be "void".

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

timurCommented:
It won't return 0. It'll return the number that happends to be in AL at exit. (At least I think Borland C does that.)
0
LevinCommented:
If anything, it should be AX not just AL, however it is compiler dependent.  But you're right, I would assume that the ANSI standard says that if it's "void main()", you'll get back some
random number.

0
timurCommented:
Nop. It is AL not AX.
When a program terminates it calls int 21h with ah=4ch and al=return value. AH must be 4ch, only AL can be returned.

Just FYI.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.