Re: First program in C - what is going on with function returns?
- From: "Bill Pursell" <bill.pursell@xxxxxxxxx>
- Date: 30 Apr 2006 00:29:50 -0700
ben.carbery@xxxxxxxxx wrote:
Hi,
I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is
how global variables and function returns work...
For example, I have a global array "char datestring[80];" which is
defined in the function speakdate. speakdate just converts a set of
integers (date variables) to a string.
The main program then does:
"printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));"
And correctly prints the value of datestring.
What I don't get is why this works since I haven't explicitly told
speakdate to return datestring.
I figured by default it would return nothing at all.
By default, it returns an int. I'm not certain, but I believe what is
happening is that the return value of the last strcat is being returned
from speakdate. strcat is returning a char *, and the char * is being
cast to an int. You're program is working because on your machine int
and char * probably have the same size.
To avoid this problem, you should explicitely specify the return type
of the function, and explicitely return a value.
.
- Follow-Ups:
- Re: First program in C - what is going on with function returns?
- From: ben . carbery
- Re: First program in C - what is going on with function returns?
- References:
- First program in C - what is going on with function returns?
- From: ben . carbery
- First program in C - what is going on with function returns?
- Prev by Date: Re: Question about "enums"
- Next by Date: Re: Question about "enums"
- Previous by thread: First program in C - what is going on with function returns?
- Next by thread: Re: First program in C - what is going on with function returns?
- Index(es):
Relevant Pages
|