-2

I am new to C and learning it for my University course. I am learning about functions and have to create a function that doesn't have any printf or scanf in it, just a function that calculates how many days are in a week.

int main(days)
{

    int weeks;

    printf("\nPlease enter a number of weeks: ");
    scanf("%i", &weeks);

    weekstodays(weeks);

    printf("\nThere are %i days in %i weeks.\n", days, weeks);
    return 0;
}
int weekstodays(weeks){

    int days;

    days = weeks * 7;
    printf("%i", days);

    return(days);

}

Whenever I build and run this, the main function outputs 1 day, but the weekstodays function outputs the desired result. (The printf in the weekstodays function is just to see the value of days) Does anyone know why the weekstodays function is not returning the day variable correctly?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    `days` in `main()` and `days` in `weekstodays()` are different variables. – pmg Mar 01 '22 at 15:26
  • 6
    `int main(days)` ???? – William Pursell Mar 01 '22 at 15:26
  • 5
    Suggestion: turn on and mind the maximum warnings your compiler can output. – pmg Mar 01 '22 at 15:27
  • 1
    `int main(days)` ought to cause at least a compiler warning. The only reason any compiler accepts that is to conform with decades old bad practice. Probably, the compiler is treating that as equivalent to `int main(int days)` (so the behavior is actually undefined) and days is just the number of arguments passed to the program. – William Pursell Mar 01 '22 at 15:28
  • 2
    Please post a [mcve] in valid C code that compiles with a working C compiler. This code does not. https://godbolt.org/z/eba3Kbe7K – Lundin Mar 01 '22 at 15:37
  • Also check out [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Mar 01 '22 at 15:38

1 Answers1

1

You are not using the returned value of the function in this statement

weekstodays(weeks);

Write

int days = weekstodays(weeks);

Pay attention to that the function declaration is incorrect

int weekstodays(weeks){

Write

int weekstodays(int weeks){

Also place one more function declaration before main.

Pay attention to that also the declaration of main is incorrect

int main(days)

According to the C Standard the function shall be declared either like

int main( void )

or

int main( int argc, char * argv[] )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335