Re: First program in C - what is going on with function returns?



[snips]

On Sat, 29 Apr 2006 23:52:57 -0700, ben.carbery wrote:

Full code below. By the way any other tips on my code would be welcomed
and appreciated!


You said "full code" - but I don't see the proper includes here.

char datestring[80];
/* dim is 'days in month'*/
int dim;

speakdate(int d, int m, int y)

What type is being returned? If you meant "nothing", specify void.

strcpy(datestring,stdate);
strcat(datestring,stmonth);

At the end of the function, the global datestring has a date. So far so
good.


int countdays(int bd, int bm, int by, int cd, int cm, int cy) {

int month,year;

int daysalive = 0;

/* count the days in whole years */
for (year=by+1;year<cy;year=++year) {

You probably don't want year=++year, but simply ++year

if (fmod(year,4)==0) {

At a guess, you simply want the modulus operand - % - here.

main(void)

int main(void)

{

int bdate,bmonth,byear;

printf ("Please enter your birth date (d/m/yyyy):\n"); scanf
("%d/%d/%d",&bdate,&bmonth,&byear);

printf ("Your date of birth is %s\n\n",speakdate(bdate,bmonth,byear));

speakdate(), as written, returns nothing, so treating it as if it does is
an error. That you get any output at all is simply a matter of accident.

printf ("Congratulations! You were born a total of %d days
ago.\n",countdays(bdate,bmonth,byear,cdate,cmonth,cyear));


[/code]

No close brace, no return. Despite the claim of "full code below",
there's no way this could be the full code, as it cannot compile.

That said, you should also turn your compiler's warning level to maximum,
and pay attention to the warnings.


.



Relevant Pages

  • Re: Is this an error or undefined behaviour?
    ... void func1 ... int x; ... ifgoto label; ... at the highest warning level it says ...
    (comp.lang.c)
  • Re: Warnings in lcc-win
    ... In that case at the default warning level it does not conform to the C standard. ... 7 assignment of pointer to long int to pointer to int ... It certainly has to produce a diagnostic in whatever mode it claims to conform to any version of the C standard. ...
    (comp.lang.c)
  • Re: is 0xE-2 a valid expression?
    ... Kenneth Brody wrote: ... int i = 0xE-2; ... usenet.c:1: missing white space after number `0xE' ... They compile without warning, even with max warning level, ...
    (comp.lang.c)
  • Re: is 0xE-2 a valid expression?
    ... Seebs wrote: ... int i = 0xE-2; ... usenet.c:1: missing white space after number `0xE' ... They compile without warning, even with max warning level, ...
    (comp.lang.c)
  • Re: Casting double to int produces inconsistent results from VS 2003 to VS 2008
    ... int d =; ... You might experiment with the /fp options, particularly /fp:fast, which IIRC most closely approximates the model used in previous versions of the compiler. ... If you compile with warning level 4, do you get warnings under both compilers? ... Anthony Wieser ...
    (microsoft.public.vc.language)