Solved

return value of main()

Posted on 1997-10-02
6
730 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now