473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(void) printf vs printf

Hi, c.l.cs

I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout \n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.

Thanks,

Whatluo.

Nov 14 '05 #1
29 17632
In article <11************ **********@g44g 2000cwa.googleg roups.com>,
whatluo <wh*****@gmail. com> wrote:
I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout \n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.


It can silence a compiler (or stand-alone "lint" program) warning
that the result of a function has been thrown away.

Also, if the programmer is being particularily careful to check
error conditions at every place errors can occur, then
a (void) on the printf() can be a signal that the lack of error
checking on that one statement is a deliberate design decision
instead of something that was overlooked.
--
History is a pile of debris -- Laurie Anderson
Nov 14 '05 #2
In article <11************ **********@g44g 2000cwa.googleg roups.com>,
"whatluo" <wh*****@gmail. com> wrote:
Hi, c.l.cs

I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout \n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.


printf returns a value.

When a function returns a value, and the programmer ignores the code, I
would want to know why. There are usually two explanations: The
programmer made a mistake ignoring the return value, or the programmer
did ignore the return value on purpose. When you write

printf (...);

I have no idea if you wanted to ignore the return value or not. If you
write

(void) printf (...);

it tells me that the programmer knew that printf returns a value, and
made a conscious decision to ignore that value. Do some professional
programming, try finding a few bugs in a million lines of other people's
code, and you will appreciate such things.
Nov 14 '05 #3
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote:
# In article <11************ **********@g44g 2000cwa.googleg roups.com>,
# "whatluo" <wh*****@gmail. com> wrote:
#
# > Hi, c.l.cs
# >
# > I noticed that someone like add (void) before the printf call,
# > like: (void) printf("Timeout \n"); while the others does't. So
# > can someone tell me whether there any gains in adding
# > (void) to printf.
#
# printf returns a value.
#
# When a function returns a value, and the programmer ignores the code, I
# would want to know why. There are usually two explanations: The
# programmer made a mistake ignoring the return value, or the programmer
# did ignore the return value on purpose. When you write

I routinely notice lots of people checking the return of printf
instead of simply looking at the terminal window.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Nov 14 '05 #4
SM Ryan <wy*****@tang o-sierra-oscar-foxtrot-tango.fake.org> writes:
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote:
# In article <11************ **********@g44g 2000cwa.googleg roups.com>,
# "whatluo" <wh*****@gmail. com> wrote:
#
# > Hi, c.l.cs
# >
# > I noticed that someone like add (void) before the printf call,
# > like: (void) printf("Timeout \n"); while the others does't. So
# > can someone tell me whether there any gains in adding
# > (void) to printf.
#
# printf returns a value.
#
# When a function returns a value, and the programmer ignores the code, I
# would want to know why. There are usually two explanations: The
# programmer made a mistake ignoring the return value, or the programmer
# did ignore the return value on purpose. When you write

I routinely notice lots of people checking the return of printf
instead of simply looking at the terminal window.


Of course, sometimes just staring at terminals may be deceptive at
times. Consider a program that prints whitespace separated words and
shows two words on your terminal:

hello world

Have these been printed by a single call to printf?

printf("hello world\n");

Two successive calls?

printf("hello ");
printf("world\n ");

Or maybe three calls, where the middle one failed, for some obscure
(and undetected by just staring at the output) reason?

printf("hello ");
printf("strange ");
printf("world\n ");

Nov 14 '05 #5
"whatluo" <wh*****@gmail. com> wrote:
I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout \n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.


Nothing. It shuts up geriatric versions of lint, that's all. But you're
better off using a lint that wasn't designed on clay tablets anyway.

Richard
Nov 14 '05 #6
"P.J. Plauger" <pj*@dinkumware .com> wrote:
Note, however, that these rules do *not* apply to casting
the value returned by malloc. So, despite the fact(s) that

-- a (void) cast is *never* needed in a conforming C program, and

-- it's only real-world effect is to silence diagnostics in
a non-standard dialect of C (lint)

it is nevertheless a Good Thing (TM) to write (void) casts and
a Bad Thing (SM) to do exactly the same thing for the return
value of malloc.
No, because of those reasons, and more, it is a Bad Thing to write
(void) casts and also a Bad Thing to cast malloc().
Go figure.


Furrfu.

Richard
Nov 14 '05 #7
int printf(...) and void printf(...)

When we use printf(...) and we don't handle the return values the
return value is destroyed with the loading and execution of the next
statement call.

When use void printf(...) we instruct the system to flush out any
return values before moving to the next statement, thus we clean up any
possibility of fetching the return value.

Positive values from the function call specifies the number of bytes
written to STDOUT, excluding '\0' in general, while a negative value
tells the error code if occured.

Nov 14 '05 #8
> Also, if the programmer is being particularily careful to check
error conditions at every place errors can occur, then
a (void) on the printf() can be a signal that the lack of error
checking on that one statement is a deliberate design decision
instead of something that was overlooked.


Interesting. I knew things like successful file openings should be
error checked, and pointers should be checked before accessing, etc.
But printfs? What is usually the accepted degree of error checking?

Nov 14 '05 #9


No Such Luck wrote:
Also, if the programmer is being particularily careful to check
error conditions at every place errors can occur, then
a (void) on the printf() can be a signal that the lack of error
checking on that one statement is a deliberate design decision
instead of something that was overlooked.

Interesting. I knew things like successful file openings should be
error checked, and pointers should be checked before accessing, etc.
But printfs? What is usually the accepted degree of error checking?


How important is the output? What are the consequences
if it fails to appear?

<whimsy>

A complete absence of error checking suggests that the
programmer places no value on the output and doesn't care
whether it appears or not. If so, the program's efficiency
can probably be improved by removing all the printf() calls,
along with the computations that produce data for them.
Following this principle, here is an optimized version of
the canonical "Hello, world!" program:

int main(void) {
return 0;
}

A great many programs can be optimized this way, and it
turns out that quite a few of them, after optimization,
become identical to the code shown above. This is a great
savings, because debugging and testing this one piece of
code essentially debugs and tests all those other programs
at the same time. A triumph for code re-use!

</whimsy>

--
Er*********@sun .com

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1951
by: Mark Antony | last post by:
Hello everyone, I am writing a function that takes a void* as an argument. In this function, there is some data that needs to be given back in the form of a void pointer. This is the simple test that I am trying to do: ////////////////////////CODE///////////////////////////////// #include <stdio.h>
2
11277
by: herrcho | last post by:
#include <stdio.h> int multi; int main() { printf("\nmulti = %p",(void *)multi); printf("\nmulti = %p",(void *)&multi); printf("\nmulti = %p",(void *)multi); printf("\n&multi = %p\n",(void *)&multi);
6
9148
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h> #include<string.h> #include<stddef.h> struct node
188
17471
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
6
3494
by: rouble | last post by:
Hi All, Is it safe to store a uchar in a void* and then extract the uchar value out of it again ? My understanding is that the size of a void* should always be equal to or greater than the size of a uchar. So, theoretically, this should be safe. Please correct me if I am wrong. I've got the following code that compiles (with warnings), and it also
4
1167
by: Notre Poubelle | last post by:
Hello, I have some code where I need to store a value as LPVOID. I'd like to be able to do some runtime testing about whether the thing that's pointed to is of a certain type. I've tried using RTTI's typeid and dynamic_cast functions (the latter won't compile), but they've not worked for me. I've included a short sample program that sort of shows what I'm trying to do and possibly reveals whether I'm doing something dumb. The...
12
5473
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
18
10437
by: planetzoom | last post by:
Given the following code: #include <stdio.h> int main(void) { char array = "What is your favorite car?"; void *vp = &array; printf("%s\n", vp);
160
5736
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.