Thread: Why wouldn't a C programmer structure their code like this?

  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33

    Why wouldn't a C programmer structure their code like this?

    To me, programming isn't really about practical problem solving as much it is about learning and figuring things out. I think general structuring practices are interesting: "how many comments should you leave?", etc. They tend to deal with the more human aspect of writing programs.

    In C, you have to either create your separate functions (outside of main) right off the bat, or prototype them and put their code after the main function. Since the main function is in every C program, it would make more sense IMO to use the second type of structure:

    Code:
    #include <stdio.h>
    /* function prototype */
    
    void prompt();
    
    int main()
    {
      int loop;
    
      char input[32];
    
      loop=0;
    
      while(loop<5)
      {
        prompt();
        fgets(input,32,stdin);
        loop=loop+1;
      }
    
      return(0);
    }
    
    /* Display prompt */
    void prompt(void)
    {
      printf("C:\\DOS> ");
    }
    However, when i look at the source code for programs that I use, i see that they prefer not to prototype the non-main functions, and just put them before main. After, it is less typing. I would think that putting the main function before all the other ones in the file would more sense for readability, but i am not much of a C programmer. Are there also other benefits to putting your non-main functions after main?

    The code above is just an example from a book, btw, to demonstrate what I'm talking about, it is pretty silly/pointless outside of an educational exercise.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    As soon as you realise that having more than one source file would be a good idea, most of the "prototype or not" arguments go away.

    > Are there also other benefits to putting your non-main functions after main?
    If you prefer a top-down view of the program, you get to see main() first just by scrolling through the code.


    There are other oddities in your short example.
    - using a while loop where a for loop would have made more sense.
    - using parentheses on the return statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Quote Originally Posted by Salem View Post
    As soon as you realise that having more than one source file would be a good idea, most of the "prototype or not" arguments go away.

    > Are there also other benefits to putting your non-main functions after main?
    If you prefer a top-down view of the program, you get to see main() first just by scrolling through the code.


    There are other oddities in your short example.
    - using a while loop where a for loop would have made more sense.
    - using parentheses on the return statement.
    Oh, well as i explained, the code isn't mine. I wouldn't put parenthesis around a return value because it's more typing. Also, i see your point about the for loop, because we wouldn't have to declare the value for the loop above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wouldn't this be against convention or something?
    By ImageJPEG in forum C Programming
    Replies: 11
    Last Post: 12-06-2017, 09:15 PM
  2. Wouldn't this be about the same as a class in C++?
    By HelpfulPerson in forum C Programming
    Replies: 27
    Last Post: 08-10-2013, 06:38 AM
  3. please help i'm so new to this you wouldn't believe
    By jaquenta in forum C Programming
    Replies: 7
    Last Post: 12-09-2008, 08:49 AM
  4. Help a vb Programmer with some directshow code?
    By CliffRichard in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2001, 07:34 PM

Tags for this Thread