473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

What ia advantage of writing (void)printf(),(void)scanf() etc.....?

Hi,
I have seen in embedded prgarmming many of using (void)printf(),(void)scanf()......like this .
printf is a function which returns an integer that is many character has been written but if we r not intersted in the return value then it is lossed.

but when we r specifying void infront of printf is that compiler ignores that return value?.......
Is ther any performance related thing with this ? I have tested time concern thing but I didn't find anything.

could anybody help me in this concept.
Feb 7 '07 #1
6 2187
Ganon11
3,652 Expert 2GB
Not completely sure what that's doing. I've always used cin and cout rather than printf and scanf...Perhaps it is taking the integer that was returned by both and typecasting it to type void - in effect, deallocating that integer from memory.
Feb 8 '07 #2
RedSon
5,000 Expert 4TB
Its type casting an integer return value to void, which basically means its nullifying the return value. AFAIK, it doesn't garner any performance gains. If anything the casting takes up more flops then just returning the int to nothingness.
Feb 8 '07 #3
AdrianH
1,251 Expert 1GB
The reason for the cast to void is to keep the compiler from complaining. There are some compilers which are very strict. These compilers will take offence at a function call return value not being used, and assumes that you did something that you didn't intend to do. Casting it to void tells the compiler that you indeed intend to ignore the return value, and so will no longer complain.

More and more, in new code, people are not allowing warnings to just happen. The reason for this is because warnings are there to WARN the programmer that they may have done something incorrect. Telling the compiler that you intended to do what you did keeps the compiler happy and allows the programmer not to get so flooded with warnings that they are ignored, possibly allowing a programmer to do something unintentional.

Oh and casting to void takes no flops. ;) In fact there are many casts that are only cosmetic and have no actual code generated from them. Doing an upcast (casting to a derived class) on a single inheritance type for instance does nothing but tell the compiler to think of the object as its more general form. It too doesn't actually generate any code. Reinterpret casts to a reference or a pointer is also of that class of casts.

Hope this helps.


Adrian
Feb 9 '07 #4
Motoma
3,237 Expert 2GB
Great post! Thanks Adrian!
Feb 9 '07 #5
RedSon
5,000 Expert 4TB
The reason for the cast to void is to keep the compiler from complaining. There are some compilers which are very strict. These compilers will take offence at a function call return value not being used, and assumes that you did something that you didn't intend to do. Casting it to void tells the compiler that you indeed intend to ignore the return value, and so will no longer complain.

More and more, in new code, people are not allowing warnings to just happen. The reason for this is because warnings are there to WARN the programmer that they may have done something incorrect. Telling the compiler that you intended to do what you did keeps the compiler happy and allows the programmer not to get so flooded with warnings that they are ignored, possibly allowing a programmer to do something unintentional.

Oh and casting to void takes no flops. ;) In fact there are many casts that are only cosmetic and have no actual code generated from them. Doing an upcast (casting to a derived class) on a single inheritance type for instance does nothing but tell the compiler to think of the object as its more general form. It too doesn't actually generate any code. Reinterpret casts to a reference or a pointer is also of that class of casts.

Hope this helps.


Adrian
Heh, who would have known its just a compiler thing. I thought it was just a programmer choice, you know some programmer's like to be extremely explicit about things. Now that I think about it I see how there is not code generated from that type of cast. Interesting!
Feb 9 '07 #6
THANKS AdrianH,
Ur information help me lot.
Feb 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
21
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
5
by: maadhuu | last post by:
hey, i want to know the exact implementation of printf function...what i do know is that the arguments are pushed into a stack from right to left and the function is called and then some pointer...
17
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been...
25
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
27
by: guoliang | last post by:
help: #include<stdio.h> int main(void) { int a; char b; printf("int=");
2
by: Xavier Serrand | last post by:
With GCC version 3.4.4 (mingw special) , following code gives stange results : void encapsulation_c(void) { double x, y; std::printf("Calcul de moyenne\n"); std::printf("Entrez le premier...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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,...
0
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...

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.