Hello.,
Im learning C online, and one of the examples from the internet is this:

#include <stdio.h>

int CalcVolume (int length, int width, int height);

void PrintIt (int vol);

void main()
{	
int volume1, volume2;
int x = 8, y = 15, z = 20;
volume1 = CalcVolume (10, 20, 30);
PrintIt (volume1);
volume2 = CalcVolume (x, y, z);		
PrintIt (volume2);	
}	

/* end of main */	

int CalcVolume (int length, int width, int height)	

{

int vol;	
vol = length * width * height;
return vol;	
}
/* end of CalcVolume */

void PrintIt (int vol)
{		
printf("The volume is: %d\n", vol);
}

but when im trying to compile it sais

ch5m.c: In function ‘main’:
ch5m.c:9: warning: return type of ‘main’ is not ‘int’

is there an error in the exesize?

Thanx!

Dani AI

Generated

The compiler warning shown in the thread is telling you that the program’s main function does not match the C standard’s required form. is correct to point out that main should be written to return an integer; that is the portable, standard-conforming fix. The C standard defines the behavior of program startup and requires main to have an int return type so the program can report an exit status to the operating system (see the C11 draft for the language on program startup).

A few important notes and clarifications beyond the replies already posted:

  • The required int return is about portability and conformity. Since C99/C11, if control reaches the end of main without an explicit return, it is treated as returning zero, but some compilers or strict flags will still warn if the declaration itself is nonstandard. See the discussion of main and implicit return semantics for details.
  • The suggestion from to use a nonstandard void main plus getch() is not portable. getch() comes from nonstandard libraries (conio.h) and is platform-specific; relying on it or on void main can hide bugs or cause different behavior across compilers and systems. For portable pausing or input, use standard I/O functions or run the program in a terminal.

Practical checklist to remove the warning and keep code portable:

  • Use a standard-conforming main signature and an integer exit status as required by the standard.
  • Compile with a modern standard and warnings enabled (for example, use flags like -std=c11 -Wall -Wextra -pedantic with GCC/Clang) to catch nonportable constructs.
  • Avoid platform-specific headers/functions (conio.h/getch) in portable examples.

References: C11 program startup text (C11 draft N1570) and a concise summary of main behavior (). For getch() platform specifics see the Microsoft docs (getch — MSVC).

Recommended Answers

All 2 Replies

Some C compiler will give you a warning when your main function does not return an integer.

In this case, do the following changes to the main function:

int main()
{
......
......
return 0;
}

you can also use the above statement as
void main()
{
........
.......
.....
getch();
}
since in using the void function you need have to use return anything.

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.