Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why typecast to void when call a function with no return value

Reply
Thread Tools

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

 
 
su
Guest
Posts: n/a
 
      01-23-2009
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-23-2009
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
 
Reply With Quote
 
 
 
 
su
Guest
Posts: n/a
 
      01-23-2009
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-23-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x) Abhishek C Programming 12 01-30-2006 03:22 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57