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: incompatible warning in C
    ... warnings, let alone errors. ... Turn up the warning level until it breaks. ... int lsp_id; ... clcm@xxxxxxxxxxxx -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. ...
    (comp.lang.c.moderated)
  • Re: ansi c
    ... char organizzatore; ... int partecipanti; ... (For non-Italian speakers, look up "cazzo" somewhere...) ... Then raise the warning level. ...
    (comp.lang.c)
  • Re: WAV file question
    ... >> matches the endianness of the file. ... > couldn't read a .wav file properly? ... The Google Groups interface makes proper followups needlessly ... by fwriteing, say, an int value, then copy the file to a big-endian ...
    (comp.lang.c)