main()...then what?

This is a discussion on main()...then what? within the C Programming forums, part of the General Programming Boards category; OK I am just starting to make some simple programs and after I have made my main thing: main() { ...

  1. #1
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    main()...then what?

    OK I am just starting to make some simple programs and after I have made my main thing:

    main()
    {
    printf("Whatever");
    return 0;
    }

    How do I make something else like main?
    Or do you only make 1 main()??

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    main is a function. You can only have one main. You can have as manu functions as you like. For example:
    Code:
    void PrintLine(void){
        printf("Hello World\n");
    }
    
    int main(void){
        PrintLine();
        return 0;
    }
    Your program starts at main. The first line calls the function PrintLine which takes nothing (void) and returns nothing, void. The first void means it returns nothing and the void in parenthesis means it takes nothing. The function, PrintLine, prints "Hello World" to the screen, moves to the next line and then returns to main. main then returns 0 and your program is over. Try a search on www.google.com for good C-Tutorials or check out my web page in my signature which contains some C-Tutorials.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Oh geez....

    What do you mean? "main" is the *required* program entry point in any C program. Still, main is simply a function. So, yes, you can define different functions and so forth, but you do not simply create a bunch of main's!

    You should ask a little more specific questions, and read some good starter books on C...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    There is only one main (AFAIK). Other functions are defined outside main. A simple example of this would be;

    Code:
    void whatever (void)
    {
    printf("Whatever");
    }
    
    int main (void)
    {
    whatever();
    return 0;
    }
    If you put your functions before main, main can call them without issue.

    I could explain lots more, but this would create more questions for you. I think you should really go back to the tutorials and work through them again.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Think of a problem you want to solve - but start small. For instance, if I asked you to write a program that calculates the volume of a cube, could you do it? Create a simple program to take a users input and outputs the result. This is how we all begin to write code. Step by step...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    For instance, if I asked you to write a program that calculates the volume of a cube, could you do it?
    Tv is going, alot's going on, but here's something off my head quick like:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main ( void )
    {
    	float Width;
    	float Height;
    	float Depth;
    	float Volume;
    	
    	printf("Enter the width ( feet are being used ):\n$");
            scanf("%f", &Width);
            
            printf("\nEnter the height ( feet are being used ):\n$");
            scanf("%f", &Height);
            
            printf("\nEnter the width ( feet are being used ):\n$");
            scanf("%f", &Depth);
            
            Volume = Height * Width * Depth;
            printf("\nThe cubic volume of this object is %f feet", Volume);
            getch();
            
            return 0;
    }
    It does the job, but could use work.
    The world is waiting. I must leave you now.

  7. #7
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Thanks a lot Shadow, I am just beginning this stuff but I actually understood most of that(except for some of the syntax but I am working on that).
    So once again, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why To Use int main()
    By year2038bug in forum C Programming
    Replies: 2
    Last Post: 08-19-2005, 01:28 PM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Int Main or Void Main?
    By FromHolland in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2003, 04:29 PM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM

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