casting printf/scanf 
Author Message
 casting printf/scanf

Quote:

> Another question from a beginner... Is there any difference
> between printf( ... ) and (void)printf( ... )? Same for scanf.
> Which form should I use? Thanks. FC.

The second form takes care of (some) compilers that can't keep their
pedantic
mouth shut. The printf() function returns a value (the number of
characters
actually written or a negative value if something went wrong). A simple
statement
like 'printf("Hello world\n");' discards this return value implicitely,
while
the second form '(void)printf("Hello world\n");' explicitely tells the
compiler
that you're not interested in printf's return value.

Both forms are perfectly legal and both generate identical code.

Ignoring the return value of a function call is AVND (A Very {*filter*}
Deed)
by itself, but I do prefer the first form because a simple function
call, not part
of another expression (including assignment expressions) tells me that
I'm simply
calling this function for its side effects (identical to Pascal's
'procedure' vs. 'function' calls). If a compiler warns about it, I
simply consider it a
'nanny-type' of warning after checking out what actually made the
compiler whine ...

kind regards,




Sat, 04 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Another question from a beginner... Is there any difference
between printf( ... ) and (void)printf( ... )? Same for scanf.
Which form should I use? Thanks. FC.



Sat, 04 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Quote:

>Another question from a beginner... Is there any difference
>between printf( ... ) and (void)printf( ... )? Same for scanf.

printf(...) ignors the functions retrun value whereas the void
cast tells the compiler you don't want to know what it was.

Quote:
>Which form should I use? Thanks. FC.

Some compilers and source code checkers may generate a warning if
you throw away the value returned by a function. With printf,
what you are primarily interested in is the what the function does,
and not what it returns (the number of characters transmitted, or
a negative value if an output error occured). Most people don't
care about the first part, and I'm not sure what the proper
response to a failure on a call to printf would be.

I think all those void casts just clutter up the code. Note that
you are still ignoring a possible failure condition. See if you
can configure your compiler or lint pakage so as not to generate
*useless* warnings.

--
Craig

Manchester, NH
I think the military is having a real problem at the
moment -- it has to train its recruits to kill, but
not to offend.   -- Arianna Huffington



Sat, 04 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Quote:

> Another question from a beginner... Is there any difference
> between printf( ... ) and (void)printf( ... )? Same for scanf.
> Which form should I use? Thanks. FC.

Compiler never differ functions from their return value, only from
their names and (in C++)argument types (overloading).
That means printf( ... ) and (void)printf( ... ) is just the same.

             - Mark



Sun, 05 Mar 2000 03:00:00 GMT  
 casting printf/scanf


Quote:

>> Another question from a beginner... Is there any difference
>> between printf( ... ) and (void)printf( ... )? Same for scanf.
>> Which form should I use? Thanks. FC.

>Compiler never differ functions from their return value, only from
>their names and (in C++)argument types (overloading).
>That means printf( ... ) and (void)printf( ... ) is just the same.

They represent two totally different kinds of expressions. A void
expression denotes a nonexistent value. That is what the cast causes
the value returned from printf to become. A plain printf call may
result in 17, or 32, ect.

--
Craig

Manchester, NH
I think the military is having a real problem at the
moment -- it has to train its recruits to kill, but
not to offend.   -- Arianna Huffington



Mon, 06 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Quote:



> >> Another question from a beginner... Is there any difference
> >> between printf( ... ) and (void)printf( ... )? Same for scanf.
> >> Which form should I use? Thanks. FC.

> >Compiler never differ functions from their return value, only from
> >their names and (in C++)argument types (overloading).
> >That means printf( ... ) and (void)printf( ... ) is just the same.

> They represent two totally different kinds of expressions. A void
> expression denotes a nonexistent value. That is what the cast causes
> the value returned from printf to become. A plain printf call may
> result in 17, or 32, ect.

At the risk of sounding overly picky: the return type of printf is int,
regardless of what casts you may apply to it. The result of a cast to
void is void, so the type of the expression (void)printf(...) is void,
but printf still returns its integer value. The cast just says to ignore
it.
        -- Pete


Mon, 06 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Quote:

> Another question from a beginner... Is there any difference
> between printf( ... ) and (void)printf( ... )? Same for scanf.

printf() returns a value (the number of characters written or an
negative number to indicate an error).  Simply calling printf() as in
your first example discards this return value.  Casting its return
value to void tells the compiler that yes, you really do intend to
ignore the result.  Some compilers and lints will warn you you are
ignoring the return value if you don't do this.

A similar thing happens with scanf() except that in this case it is
meaningful to check the return value because it tells you the number
of fields successfully read in.  If you must use scanf() for input,
then you should make every effort to make sure you handle *all* the
input correctly.

Quote:
> Which form should I use? Thanks. FC.

For printf(), it's a matter of taste.  Most people find the cast
overly picky, and few compilers or lints will warn about it's
omission.  For scanf() the result can be used meaningfully, so it
shouldn't be being ignored.

--

            http://www.geocities.com/SiliconValley/Lakes/7537/



Wed, 08 Mar 2000 03:00:00 GMT  
 casting printf/scanf

Quote:




>> >> Another question from a beginner... Is there any difference
>> >> between printf( ... ) and (void)printf( ... )? Same for scanf.
>> >> Which form should I use? Thanks. FC.

>> >Compiler never differ functions from their return value, only from
>> >their names and (in C++)argument types (overloading).
>> >That means printf( ... ) and (void)printf( ... ) is just the same.

>> They represent two totally different kinds of expressions. A void
>> expression denotes a nonexistent value. That is what the cast causes
>> the value returned from printf to become. A plain printf call may
>> result in 17, or 32, ect.

>At the risk of sounding overly picky: the return type of printf is int,
>regardless of what casts you may apply to it. The result of a cast to
>void is void, so the type of the expression (void)printf(...) is void,
>but printf still returns its integer value. The cast just says to ignore
>it.

It would have been clearer if I had written "the value of the
expression", rather than "the value returned from printf". If printf
returned 17, you would have (void)17.

Interestingly, Mark's comment about printf(...) and (void)printf(...)
being the same, with a slight shift in context (a'la SN), has a grain
of truth to it: They probably result in the same code being generated.

It takes something like int i = printf(...) to get the returned value
popped off the stack (you should probably expect this). The void cast
is working at the C language level, not at the level of processor
intstructions.

--
Craig

Manchester, NH
Maybe a great magnet pulls/All souls towards truth
Or maybe it is life itself/That feeds wisdom/To its
youth.   -- k.d. lang



Wed, 08 Mar 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

2. Simple scanf/printf question

3. skipped printf/scanf ???

4. tiny question on format of scanf and printf

5. Examples of format strings for scanf/printf ?

6. scanf/printf conversions

7. pointers in scanf and printf

8. Beginner question - printf and scanf

9. printf() and scanf()

10. Q:printf() and scanf()....

11. printf and scanf in IBMC on IBM 3090

12. printf/scanf trouble on SPARCstation

 

 
Powered by phpBB® Forum Software