return value of functions

Discussion in 'C Programming' started by yohji, Apr 9, 2005.

  1. yohji

    yohji Guest

    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??
    yohji, Apr 9, 2005
    #1
    1. Advertising

  2. yohji

    Minti Guest

    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,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?

    --
    Imanpreet Singh Arora
    Minti, Apr 9, 2005
    #2
    1. Advertising

  3. 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++
    Emmanuel Delahaye, Apr 9, 2005
    #3
  4. 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,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.
    Martin Ambuhl, Apr 9, 2005
    #4
  5. "Emmanuel Delahaye" <> 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_Keith) <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, Apr 9, 2005
    #5
  6. Keith Thompson wrote on 09/04/05 :
    > "Emmanuel Delahaye" <> 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++
    Emmanuel Delahaye, Apr 9, 2005
    #6
  7. yohji

    yohji Guest

    Er,I see._int_ functions in C are special,aren't they?
    yohji, Apr 12, 2005
    #7
  8. yohji

    yohji Guest

    Er,I see._int_ functions in C are special,aren't they?
    yohji, Apr 12, 2005
    #8
  9. "yohji" <> writes:
    > Er,I see._int_ functions in C are special,aren't they?


    Not particularly. What do you mean?

    --
    Keith Thompson (The_Other_Keith) <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, Apr 12, 2005
    #9
  10. yohji

    Richard Bos Guest

    "yohji" <> 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
    Richard Bos, Apr 12, 2005
    #10
  11. yohji

    CBFalconer Guest

    yohji wrote:
    >
    > 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.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Apr 12, 2005
    #11
  12. Keith Thompson wrote:
    > "Emmanuel Delahaye" <> 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,

    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.

    --
    Peter
    Peter Nilsson, Apr 12, 2005
    #12
  13. "Peter Nilsson" <> writes:
    > Keith Thompson wrote:
    >> "Emmanuel Delahaye" <> 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,
    >
    > 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.


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

    --
    Keith Thompson (The_Other_Keith) <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, Apr 12, 2005
    #13
  14. On Sat, 09 Apr 2005 12:16:21 +0200, "Emmanuel Delahaye"
    <> wrote:

    <snip>
    > 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
    Dave Thompson, Apr 18, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,964
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
  3. Seong-Kook Shin
    Replies:
    1
    Views:
    479
    Richard Bos
    Jun 18, 2004
  4. Greenhorn
    Replies:
    15
    Views:
    793
    Keith Thompson
    Mar 6, 2005
  5. Sara

    Functions> call and return value

    Sara, Apr 24, 2007, in forum: C Programming
    Replies:
    16
    Views:
    541
Loading...

Share This Page