Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: C programming problem

  1. #1

    Default C programming problem

    Hello,

    As a test I made this program in vim
    Code:
    #include <stdio.h>
    
    int  main ( )
    {
            printf ("hello world \n");
            return 0 ;
    }                               /* ----------  end of function main  ---------- */
    When I deleted the o after return I see this message appear
    Code:
    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

  2. #2
    Join Date
    Jul 2009
    Location
    Rockyford Alberta Canada
    Posts
    1,388

    Default Re: C programming problem

    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 {...}

    Quote Originally Posted by snowman1967 View Post
    Hello,

    As a test I made this program in vim
    Code:
    #include <stdio.h>
    
    int  main ( )
    {
            printf ("hello world \n");
            return 0 ;
    }                               /* ----------  end of function main  ---------- */
    When I deleted the o after return I see this message appear
    Code:
    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
    When your up to your a** in Alligators it's pretty hard to remember you intended to drain the swamp (author unknown)

  3. #3

    Default Re: C programming problem

    Oke,

    I thought in C++ you can do

    void main () {
    return;
    }

    Roelof

  4. #4
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,654

    Default Re: C programming problem

    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

  5. #5
    Join Date
    Aug 2008
    Location
    Behind the 8 ball
    Posts
    116

    Default Re: C programming problem

    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.

    Code:
    #include <stdio.h>
    
    void main ( ) {
            printf ("hello world \n");
            return;
    }

  6. #6

    Default Re: C programming problem

    Hello,

    Every one thanks for the help
    When I changed it to void I see this message :
    Code:
    test.c|21 col 7| warning: return type of main is not int

  7. #7
    Join Date
    Aug 2008
    Location
    Behind the 8 ball
    Posts
    116

    Default Re: C programming problem

    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...

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

  8. #8

    Default Re: C programming problem

    Quote Originally Posted by martin_helm View Post
    In principle you can define the main function also as void, this is explicitly allowed by the C standard.
    Not that I have access to a copy of the official standard. But from http://www.open-std.org/jtc1/sc22/WG...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.

  9. #9
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: C programming problem

    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.

  10. #10
    Join Date
    Nov 2010
    Location
    Ελλάδα(Αθήνα)-Россия (г. Красноярск)
    Posts
    1,711

    Default Απ: Re: C programming problem

    Code:
    #include <stdio.h>
    
    void main ( ) {
            printf ("hello world \n");
            return 0;
    }
    If you have
    [/CODE]int main()[/CODE]
    you should write it as
    Code:
    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

    Code:
    #include <stdio.h>
    
    void hello();
    
    int main () {
            hello();
            return 0;
    }
    
    void hello () {
            printf ("hello world \n");
            return;
    }
    In my opinion it will be better with that way
    Code:
    #include <stdio.h>
    void hello();
    {
      printf ("hello world \n");
            }
    int main () {
            hello();
            return 0;
    }
    Πάντα Φιλικά, Στάμος.
    Desktop: openSUSE Leap 15.3||x86_64||Gnome 3.14 & KDE 5||AMD Ryzen 7 8 core||Nvidia GTX 1080 Ti Gaming Edition
    Γλώσσες Προγραμματισμού: C++, Qt developing.
    http://bit.ly/fT8Hfi

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •