Home > Net >  Why won't the newline print? The commented part doesn't part
Why won't the newline print? The commented part doesn't part

Time:09-22

The line //if(date == 7){ fails to run the inner block and I don't understand why.

#include <stdio.h>

int main(void){
  int day,date,days,start_date,first_week_start;

  printf("\nEnter number of days in the month:");
  scanf("%d", &days);
  printf("Enter the day the calendar starts (Sun = 1, Sat = 7):");
  scanf("%d", &start_date);
  printf("\n");
  
  while(start_date > 1){
    printf("  ");
    start_date = start_date - 1;
    date = date   1;
  };

  for(day = 1; days >= day;   day,   date ){
    int day_of_the_week;
    // While the day of the week is less tha 7, number are printed
    // Once date 7 is met, prints on a new line and date is reset 
    printf("%d  ", day);
    //if(date == 7){  >>>Not Working
    if(date % 7 == 0){
      printf("Newline \n");
      date = 0;
    };
  };
};

CodePudding user response:

As the date variable is not initialized, you're encountering undefined behavior.

Strongly suggest you declare and initialize variables when you need them, and restrict the scope of variables when possible.

  •  Tags:  
  • c
  • Related