C programming problem

Hello,

As a test I made this program in vim


#include <stdio.h>

int  main ( )
{
        printf ("hello world 
");
        return 0 ;
}                               /* ----------  end of function main  ---------- */

When I deleted the o after return I see this message appear


test.c|24 col 2| warning: ‘return’ with no value, in function returning non-void

Is this normal or is the vim interpreter wrong here ?

Roelof

int main () expects to return a value as to success or not
So, yes you need to return a value in the return statement for proper compilation to occur.
return 0 says function exited normally with no error making possible other tests after the function exits to know what to do as in
if funct1() {…} else {…}

Oke,

I thought in C++ you can do

void main () {
return;
}

Roelof

In principle you can define the main function also as void, this is
explicitly allowed by the C standard. But as long as you run your program on
top of an operating system, which expects to get a status value back and not
as a C program in an embedded system on the bare metal without operating
system (for example a simple microcontroller), it would be bad style to do
that.


PC: oS 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.6.2 | GeForce
9600 GT | 4GB Ram
Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.6.0 | nVidia
ION | 3GB Ram

You are creating an inteteger function called main.
Since you declared it an integer you must return an integer.

If you don’t want to return anything you should declare the function void like snowman1967 has suggested.


#include <stdio.h>

void main ( ) {
        printf ("hello world 
");
        return;
}

Hello,

Every one thanks for the help
When I changed it to void I see this message :


test.c|21 col 7| warning: return type of ‘main’ is not ‘int’   

Sorry, I probably caused more confusion.
main is a special function and should always be an integer.
I shouldn’t have used it as an example of a void function.
This is a better example, I hope…


#include <stdio.h>

void hello();

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

void hello () {
        printf ("hello world 
");
        return;
}

Not that I have access to a copy of the official standard. But from http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

5.1.2.2.1 Program startup
1
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* … */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char argv]) { / … */ }
or equivalent;9) or in some other implementation-defined manner.

It also depends on whether you are talking about C++ or C. Apparently it comes down reading the standard very carefully to discover that void main is not disallowed in C.

FGA: “void main()” is not legal in C++ but is legal in C

Nonetheless, I personally recommend always using int main so that you don’t run into complaints from the compiler and prototype libraries. It’s not hard to put just return 0; as a last executable statement of the function if you can’t think of anything else to do. I think the compiler is also smart enough to know that exit(n) will not return, so that will also do as a last executable statement.

#include <stdio.h>

void main ( ) {
        printf ("hello world 
");
        return 0;
}

If you have

int main()

you should write it as

int main(int argc, char *argv])

Void is a return type of the main function void means not returning any thing.

Also I see this program

#include <stdio.h>

void hello();

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

void hello () {
        printf ("hello world 
");
        return;
}

In my opinion it will be better with that way

#include <stdio.h>
void hello();
{
  printf ("hello world 
");
        }
int main () {
        hello();
        return 0;
}

No you can not do this in C++.

You can do this

void main()
........
...... //*with .... I mean commands*//
return 0 //*0 or 1 or 2 or anything else*//
.......
.......
}

Sorry my mistake

#include <stdio.h>
void hello();
{
  printf ("hello world 
");
        }
void main () {
        hello();
        return 0;
}

Now the new program is ok. I had let my program with int main(), but the correct is int main(int argc, char *argv]). Anyway with void main() is ok.

void main () {
        hello();
        return 0;
}

I hope this is just a typo on your part otherwise I have to conclude that you just like putting your foot in it. Do you actually test compile the code before you post I wonder? I do for code I post, and I’ve been coding for years.

FYI in 4 lines you manage to specify void as the return type, then you return 0. That causes a warning. Really. Try it. The other warning you will get is that int main is expected.

Yeahh, you are right

void main () {
        hello();
        return 0;
}

It is wrong typo or wrong coding. I have tried to run in Dev C/C++. And I come to correct it.
I thing also to write it without return 0 because it has something to return. When we call the hello() function, the hello() functions has a “Hello World”, so something has return from function to main program.
So it will be better to write in the beginning of program include<stdlib.h> and to the end a system(“pause”), So it will be better for us to see How program is running.
It will be better to write only in printf("Hello World
") without return <something>

Hi,

As has been mentioned, it’s good practice to return an integer from the
main function, in order to provide some information on the success of
the program.

I would like to point out the following:

On Sat, 7 May 2011, stamostolias wrote:
>> If you have
>>
> --------------------
> int main()
> --------------------
> you should write it as
>
> Code:
> --------------------
> int main(int argc, char *argv])
> --------------------

There is nothing more ‘correct’ about writing the main function with argc
and argv, and actually if your program is not using any command-line
arguments they are doing absolutely nothing but wasting your time to
write them.

Also, as the return type int is implied in C, you could write:

main()
{
puts(“Hello world!”);
return 0;
}

But I prefer to write the return type for every function.


Regards,
Barry D. Nichols

On Sat, 7 May 2011, stamostolias wrote:
> So it will be better to write in the beginning of program
> include<stdlib.h> and to the end a system(“pause”), So it will be better
> for us to see How program is running.

system(“pause”) will not show you how the program is running!!

What extra information do you think your program gives you because you
paused it?

If you really want to watch the details of how the program is running,
step through it in a debugger, I recommend gdb.


Regards,
Barry D. Nichols

Maybe I have not written it well. Sorry for that. The word key who I wanted to use is to “watch the details” the running details( I mean system(“Pause”).

There is nothing more ‘correct’ about writing the main function with argc
and argv, and actually if your program is not using any command-line
arguments they are doing absolutely nothing but wasting your time to
write them.

There are no arguments but a different solution of same problem.

About
int main(int argc, char *argv])

is from compiler. Anyway it will be better to write it.

According to standards, you should return an integer from main.

5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* … */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char argv]) { / … */ }
or equivalent;9) or in some other implementation-defined manner.

This is from the ISO/IEC 9899:1999 (C99) standards document.

www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf