Why typecast to void when call a function with no return value

Discussion in 'C Programming' started by su, Jan 23, 2009.

  1. su

    su Guest

    I found in old code that when calling a void function:

    void
    foo() {
    ......
    }
    ....
    (void) foo();

    I wonder why (void) is used to typecast the function.
    Is there any historical reason?


    Any reply from you will be appreciated.
     
    su, Jan 23, 2009
    #1
    1. Advertising

  2. su

    Ian Collins Guest

    su wrote:
    > I found in old code that when calling a void function:
    >
    > void
    > foo() {
    > .....
    > }
    > ...
    > (void) foo();
    >
    > I wonder why (void) is used to typecast the function.
    > Is there any historical reason?
    >

    "cast", actors get "typecast" :)

    The reason is usually to suppress compiler or lint warnings, but they
    tend not to be a problem with functions returning void.

    --
    Ian Collins
     
    Ian Collins, Jan 23, 2009
    #2
    1. Advertising

  3. su

    su Guest

    Thanks. I got it.

    Jack Klein wrote:
    > On Thu, 22 Jan 2009 19:23:07 -0800 (PST), su <> wrote
    > in comp.lang.c:
    >
    > > I found in old code that when calling a void function:
    > >
    > > void
    > > foo() {
    > > .....
    > > }
    > > ...
    > > (void) foo();
    > >
    > > I wonder why (void) is used to typecast the function.
    > > Is there any historical reason?
    > >
    > >
    > > Any reply from you will be appreciated.

    >
    > There's no good reason at all to do this if foo() actually has a
    > "void" return type.
    >
    > There are reasons for doing this is foo() actually returns a value of
    > some type. Some static code checking tools and coding standards
    > specify that the return value of every function should be checked or
    > specifically ignored.
    >
    > So given:
    >
    > int f(int arg);
    >
    > ...then:
    >
    > f(12);
    >
    > ...violates the coding standard or generates a message from the code
    > checking tool, whereas:
    >
    > (void)f(12);
    >
    > ...does not.
    >
    > The first case could be the programmer making an error and forgetting
    > to check the return value.
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://c-faq.com/
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    su, Jan 23, 2009
    #3
  4. su

    CBFalconer Guest

    Jack Klein wrote:
    > su <> wrote in comp.lang.c:
    >

    .... snip ...
    >
    >> (void) foo();
    >>
    >> I wonder why (void) is used to typecast the function.
    >> Is there any historical reason?

    >

    .... snip ...
    >
    > int f(int arg);
    >
    > ...then:
    >
    > f(12);
    >
    > ...violates the coding standard or generates a message from the
    > code checking tool, whereas:
    >
    > (void)f(12);
    >
    > ...does not.
    >
    > The first case could be the programmer making an error and
    > forgetting to check the return value.


    And the number of scanfs and similar calls one sees unchecked
    indicate that insistance on the void usage might be helpful.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Jan 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    602
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    836
    The Real OS/2 Guy
    Oct 28, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    5
    Views:
    881
    S.Tobias
    Jul 22, 2005
  5. Abhishek
    Replies:
    12
    Views:
    841
    Eric Sosman
    Jan 30, 2006
Loading...

Share This Page