Small matter1

This is a discussion on Small matter1 within the C Programming forums, part of the General Programming Boards category; i would be glad if someone take a look of it. i'm learning c recently and i found out that ...

  1. #1
    Newmind
    Guest

    Small matter1

    i would be glad if someone take a look of it.
    i'm learning c recently and i found out that some of the program that have int main() but without the void. i need to know why there is such a void in there?would there be someone who can help me? if u can,pls write a simple program that is without a void and with a void. count on u guyss, i just wanna to make myself more clearer about it.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    It's nothing earth shattering, just style. Look up what 'void' means in a dictionary, and it will hopefully make sense.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    (void) in this context is an inherrited feature from old sytle C. It's still perfectly valid, but redundent in C++, so not required by C++ capable compilers.
    Some strict C compilers still require it (like one I use at work) so I am still in the habit of using it.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by bit_cruncher
    (void) in this context is an inherrited feature from old sytle C. It's still perfectly valid, but redundent in C++, so not required by C++ capable compilers.
    Some strict C compilers still require it (like one I use at work) so I am still in the habit of using it.
    umm, hello? look what board you are in. this is not the C++ board, so (void) is not redundant. int main () and int main (void) mean different things. in the case of main, it doesnt matter, but for functions in general (when declaring or prototyping them) get in the habit of using (void) and not ().
    hello, internet!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Cool

    In light of Moi's comment:
    Perfectly valid answer to a reasonable question about the way things are done in C - the the (void) is a required part of strict C syntax, which is why I say I do have to use it and, why this message is on this board.
    Moi darling, in this context the two situations are equivalent - so please enlighten me in your school of thougt as you clearly know something I do not, and clearly you need to be corrected. Read answers before you place sarcastic comments about them

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    like i said the difference between () and (void) doesnt really matter for main. but try this code:
    Code:
    int myfunc1 (void);
    int myfunc2 ();
    
    int myfunc1 (void)
    {
      return 55;
    }
    int myfunc2 ()
    {
      return 66;
    }
    
    int main (void)
    {
      int r = 595;
      printf ("%i %i %i %i",
        myfunc1 (),
        myfunc2 (),
        myfunc1 (r), // oops we forgot what parameters our function takes, but dont worry, the compiler will warn us
        myfunc2 (r)); // oops again, but this time, the compiler will not warn us
      return 0;
    }
    hello, internet!

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    A good point Moi, but where am I to try this code? This should be true of a C++ compliant compiler, but in strict and pure C compiler, the prototye,
    int myfunc2 ();
    is invalid whereas,
    int myfunc2 (void);
    is correct.
    However - prototyped functions are not required by ANSI C so your call to myfunc2 could result in unexpected results, provided it were not prototyped (code practice - which you have avoided). Another good reason as to why one should always get in the habit of using (void) AND using prototypes, which are compulsary in C++ (in which case this should have resulted in an overloaded function/link error depending on your compiler)

    Besides, the question was - why does the (void) appear in some C texts/examples and not others? I feel my duty is to help others learn without confusing, you have to admit this is a rather strange observation to beginners - we were all there once remember?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by bit_cruncher
    Besides, the question was - why does the (void) appear in some C texts/examples and not others?
    The answer is the same for the question "Why do some teachers use 'void main', and some use 'int main'? Why do some teachers use 'fflush(stdin)' and some not?"

    The answer is simple: Some teachers don't know what they're talking about, some do.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 03:09 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

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