Hi,everyone!
I am a beginner of C.When I studied C,I found a problem which isn't
described in many C teaching books.It is like this: I found some of int
or char* functions can be used as void function.For example: we can use
int function printf() like this :
printf("hello,w orld!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buff er,10);
Why?? 13 2610
yohji wrote: Hi,everyone! I am a beginner of C.When I studied C,I found a problem which isn't described in many C teaching books.It is like this: I found some of
int or char* functions can be used as void function.For example: we can
use int function printf() like this : printf("hello,w orld!\n"); How it is like a void function! Also we can use char* function ltoa()like this: ltoa(ltemp,buff er,10); Why??
Because, you want to. There are languages in which it is _prohibited_
to discard the return value of a function. C isn't one of them. Also a
lot of programmers, do tend to write the above "functions" as
(void) printf("Hello World!\n");
(void) scanf("%d", &value);
To indicate that _they_ know that they are discarding the value.
BTW, What is the "function" thing doing above?
--
Imanpreet Singh Arora
yohji wrote on 09/04/05 : Hi,everyone! I am a beginner of C.When I studied C,I found a problem which isn't described in many C teaching books.It is like this: I found some of int or char* functions can be used as void function.
Let's rephrase is more accurately...
C functions car return somethinf or no. When a function returns
nothing, its return type is 'void'. On the other cases, there is a
return type ('int', 'char *', or whatever...).
But when you use a non-void function, you don't /have to/ use its
returned value (but in many cases, you should, because the value has
probably some important meaning).
On the contrary, getting a value from a function returning nothing
invokes an undefined behaviour (IOW, a bug)
For example: we can use int function printf() like this : printf("hello,w orld!\n"); How it is like a void function! Also we can use char* function ltoa()like this: ltoa(ltemp,buff er,10); Why??
It depends on your paranoid level...
Note that ltoa() is not a standard C function.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
yohji wrote: Hi,everyone! I am a beginner of C.When I studied C,I found a problem which isn't described in many C teaching books.It is like this: I found some of int or char* functions can be used as void function.For example: we can use int function printf() like this : printf("hello,w orld!\n"); How it is like a void function!
You can always discard the value of an expression. That is all that is
being done here. You are calling printf for its side-effect, not for
its value. Similarly, the expression 'x++' has both a value and a
side-effect and can be used in the statement
x++;
discarding the value, caring only about the side-effect (incrementing x).
Note that 'a = b' is also an expression yielding a value, but you would
not consider it particularly strange to see
a = b;
discarding the value of the expression and using only the side-effect of
assigning the value of b to a.
Also we can use char* function ltoa()like this: ltoa(ltemp,buff er,10);
There is no standard function named 'itoa' and any syntax or semantics
are implementation-defined. If a function returns a char *, that could
be any of
a) a pointer to (or into) a buffer specified by the argument list,
b) a pointer to a static buffer
c) a pointer to a dynamically allocated buffer.
d) an error indication (e.g. NULL)
If case (c) holds, then discarding the return value will lead to a
memory leak, and you will feel very silly.
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...] On the contrary, getting a value from a function returning nothing invokes an undefined behaviour (IOW, a bug)
Hmm. I can't think of a straightforward way to (attempt to) get a
value from a void function. For example, the following won't compile,
so it has no opportunity to invoke UB.
void foo(void) {
}
int main(void)
{
int result = foo();
return result;
}
You could do it by casting a function pointer (which would invoke UB);
was that what you had in mind?
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote on 09/04/05 : "Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes: [...] On the contrary, getting a value from a function returning nothing invokes an undefined behaviour (IOW, a bug)
Hmm. I can't think of a straightforward way to (attempt to) get a value from a void function. For example, the following won't compile, so it has no opportunity to invoke UB.
void foo(void) { }
int main(void) { int result = foo(); return result; }
You could do it by casting a function pointer (which would invoke UB); was that what you had in mind?
No. I did think it was not a constraint violation and that it may
compile with some warning.
gcc:
main.c: In function `main':
main.c:16: void value not ignored as it ought to be
Borland C 3.1:
Compiling ..\MAIN.C:
Error ..\MAIN.C 16: Value of type void is not allowed
Both are rejected. Fine.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Er,I see._int_ functions in C are special,aren't they?
Er,I see._int_ functions in C are special,aren't they?
"yohji" <cc***@126.co m> writes: Er,I see._int_ functions in C are special,aren't they?
Not particularly. What do you mean?
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"yohji" <cc***@126.co m> wrote: Er,I see.
I don't. Post with context, dammit! Google's inadequacy is not an excuse
for you to emulate them.
_int_ functions in C are special,aren't they?
Well... yesno. If a function is declared without a return type, or used
without being declared, then the return type is assumed to be int. This
makes functions that _do_ return int somewhat special, since they're
automatically recognised correctly; but this is the result of this,
historically motivated, rule, not because of some magic in int functions
themselves.
This is all true for C89, but not for C99, BTW.
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Bailo |
last post by:
The c# *return* statement has been bothering me the past few months.
I don't like the fact that you can have different code paths in a method
and have multiple return statements. To me, it would be more orthogonal
if a method could only have one return statement.
--
|
by: Maximus |
last post by:
Hi,
I was just wondering, is it good to use return without arguments in a void
function as following:
void SetMapLayer()
{
if( !Map ) return;
layer = LAYER_MAP;
}
|
by: DaKoadMunky |
last post by:
Please consider the following...
<CODE>
#include <string>
using namespace std;
typedef int PrimitiveType;
typedef string ClassType;
|
by: Ximo |
last post by:
Can I do a function which don't return anything?
The question is that, if I do a function that have a return or without
return, it returns always "None", but i want that it doesnt return me
nothing
Thanks
|
by: Ann Huxtable |
last post by:
I have the following code segment - which compiles fine. I'm just
worried I may get run time probs - because it looks like the functions
are being overloaded by the return types?. Is this Ok: ?
template <class T1, class T2>
int getValue( T1 col, T2 row ) ;
template <class T1, class T2>
double getValue( T1 col, T2 row ) ;
| |
by: Marcus Jacobs |
last post by:
Dear Group
I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as...
|
by: G Patel |
last post by:
Hi,
If I want to call functions that don't return int without declaring
them, will there be any harm?
I only want to assign the function(return value) to the type that it
returns, so I don't see how the return value comes to play here.
Ex
|
by: Angel |
last post by:
Hi again,
I'm trying to call functions from a proprietary DLL but it's turned out to
be more difficult than I thought.
I have this W32.DLL which was written in C by USPS. They don't provide the
code so I only have the documentation.
I'm trying to call a function called z4date that, according to the docs,
returns the date as "an 8-byte character string in the "YYYYMMDD" format".
When I run it with this code I've written , I get "Can not...
|
by: barcaroller |
last post by:
I am trying to adopt a model for calling functions and checking their return
values. I'm following Scott Meyer's recommendation of not over-using
exceptions because of their potential overhead.
Here's the approach I'm currently looking at. I throw exceptions only from
constructors. Destructors, of course, do not throw exceptions. All other
functions return a signed integer. The values are all stored in one large
header file (as...
|
by: sanjay.vasudevan |
last post by:
Why are the following declarations invalid in C?
int f();
int f();
It would be great if anyone could also explain the design decision
for such a language restricton.
Regards,
Sanjay
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
| |
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |