I am studying C from a book and I try various stuff in order to understand the language better.
I am attempting to pass a value from a function to a global struct. I am having trouble because I perform a printf
to see if the values were passed indeed but the results show different values:
#include <stdio.h>
void receive_date(); //prototype of the function
struct date_example {
int day;
int month;
int year;
};
int main()
{
struct date_example d;
receive_date();
//this line below shows different values than those provided by the scanf inside the function
printf("the day is %d and the month is %d and the year is %d",d.day,d.month,d.year);
return 0;
}
void receive_date(void)
{
struct date_example d;
printf("give day: \n");
scanf("%d", &d.day);
printf("give month: \n");
scanf("%d", &d.month);
printf("give year: \n");
scanf("%d", &d.year);
printf("the date is: %.1d %.1d %.1d \n", d.day, d.month, d.year );
}
the result is the following:
give day:
2
give month:
3
give year:
2021
the date is: 2 3 2021
the day is 32758 and the month is 0 and the year is 0 // this is the part that gives different values
Process finished with exit code 0