Very Easy C

This is a discussion on Very Easy C within the C Programming forums, part of the General Programming Boards category; Hello Guys! What i am trying to find out is what is the difference when a function returns an integer ...

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Question Very Easy C

    Hello Guys!

    What i am trying to find out is what is the difference when a function returns an integer like in

    Code:
    int main( )
    {
    
    }
    and when it returns nothing, like void

    what do they mean by "it returns an integer"! Maybe it is too simple to find out but still, I do not get it!


    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    what do they mean by "it returns an integer"!
    Basically, that it's int main() and not void main(). Sometimes that phrase also means that you should add
    Code:
    return 0;
    at the end of main(), though in C99 (the newest version of C), you can leave it out.

    This FAQ contains a brief description: http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    There are other, more in-depth threads around if you look for them, such as this C++ one: " void main() is evil. Use int main(). "
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why do people still use main() or main(void)?

    I attempted to answer your question already in this topic. Hope it helps.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    14
    I'm trying to fully understand the int main() myself. The post refered to above is the one I actually started. This little program that I have included may help in your understanding.

    Code:
    /* Program to calculate the product of two numbers. */
      #include <stdio.h>
    
    int a,b,c;
    
    int product(int x, int y);
    
    int main()
    {
        /* Input the first number */
        printf("Enter a number between 1 and 100: ");
        scanf("%d", &a);
        /* Input the second number */
        printf("Enter another number between 1 and 100: ");
        scanf("%d", &b);
    
        /* Calculate and display the product */
        c = product(a, b);
        printf ("%d times %d = %d\n", a, b, c);
        return 0;
    }
    /* Function returns the product of its two arguments */
    int product(int x, int y)
    {
        return (x * y);
    }
    The program above points out that there is another function nested in the main function which returns an integer. I'm also still trying to grasp why you would want main() to return an integer unless it was to be called by another program and you wanted to know if the program was true or false.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because you should tell the invoker how your program ran, whether it failed or worked, or it is supposed to return some value indicating an error, so the invoker knows what's going on.

    Also consider, a simple program to add 2 numbers
    Code:
    #include <stdlib.h>
    
    /* Note the lack of error checking, but it's for example's sake */
    int main(int argc, char *argv[])
    {
        if(argc < 3)
            return 0;
        return (atoi(argv[1]) + atoi(argv[2])); 
    }
    for example:
    Code:
    ./add 5 10
    = 15
    I would suggest a read of: http://www.eskimo.com/~scs/readings/...in.960823.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 04:28 AM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21