Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

return value of main()

Posted on 1997-10-02
6
Medium Priority
?
742 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:ny971181
6 Comments
 
LVL 1

Accepted Solution

by:
timur earned 0 total points
ID: 1255457
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
 
LVL 1

Author Comment

by:ny971181
ID: 1255458
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
 
LVL 10

Expert Comment

by:rbr
ID: 1255459
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:timur
ID: 1255460
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
 

Expert Comment

by:Levin
ID: 1255461
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
 
LVL 1

Expert Comment

by:timur
ID: 1255462
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question