return value of functions

Y

yohji

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,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,10);
Why??
 
M

Minti

yohji said:
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,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,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?
 
E

Emmanuel Delahaye

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,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,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++
 
M

Martin Ambuhl

yohji said:
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,world!\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,buffer,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.
 
K

Keith Thompson

Emmanuel Delahaye said:
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?
 
E

Emmanuel Delahaye

Keith Thompson wrote on 09/04/05 :
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++
 
R

Richard Bos

yohji said:
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
 
C

CBFalconer

yohji said:
Er,I see._int_ functions in C are special,aren't they?

If you insist on posting in this newsgroup without following common
practices (such as quoting adequate context, bottom posting and
snipping) you may end up being totally ignored. If you must use
the foul google interface, at least pay heed to the instructions in
my sig, below.

The idea is to make it easy for others to understand you, not to
make it impossible. Failure to do so is extremely rude.
 
P

Peter Nilsson

Keith said:
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,

Do you have chapter and verse on that?
so it has no opportunity to invoke UB.

void foo(void) {
}

int main(void)
{
int result = foo();
return result;
}

The mere fact that there is a constraint violation does not mean
that implementations will necessarily refuse to further translate
the program. Indeed, I have only one (outdated) implementation
that refuses to compile...

char *x = 0xFFFF;

....in conforming mode.

Of course, I can't see a _good_ reason for an implementation
translating Keith's example.
 
K

Keith Thompson

Peter Nilsson said:
compile,

Do you have chapter and verse on that?


The mere fact that there is a constraint violation does not mean
that implementations will necessarily refuse to further translate
the program.

Good point. The standard requires a diagnostic; it doesn't forbid
translation. (But if a compiler successfully translated that code, I
would whine bitterly.)
 
D

Dave Thompson

On Sat, 09 Apr 2005 12:16:21 +0200, "Emmanuel Delahaye"

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).
Correct.
On the contrary, getting a value from a function returning nothing
invokes an undefined behaviour (IOW, a bug)
Corrected elsethread: trying to use the value of a function returning
void, or indeed other void expression, is a constraint violation.

However, trying to use the value of a function whose type has a return
value, but on a particular execution does not, is UB. This can occur
by "falling off" the end of the body or in C89 only by executing a
return statement with no expression. (In C99 the latter is a CV; in
both, a return with an expression in a void function is.)

Except, as a special case not actually within C code, falling off
main() (for the initial call) in C89 produces an unspecified (and
possibly wrong) exit status, but not UB; and in C99 produces the same
exit status as 0 (= success).

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top