-4

sorry, might be really dumb just started to learn C. it's a basic check if the input year is a leap year or not. I don't understand where I am going wrong. the executable starts but as soon as I input a year and press enter it closes.

I tried to remove the return 0; by using void main instead cause I thought it is causing the abrupt crash. I don't know enough to try anything else.

#include <stdio.h>

int main() {
   int year;
   printf("Enter year below\n");
   scanf("%d", &year);
   if (year % 4 == 0) {
       if (year % 100 == 0) {
           if (year % 400 == 0)
               printf("%d is a Leap year", year);
           else
               printf("%d is not a Leap year", year);
       } else
           printf("%d is a Leap year", year);
   } else
        printf("%d is not a Leap year", year);

    return 0;
}

I expected it to be able to check the leap year but it just crashes.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
pujeet jha
  • 11
  • 2

3 Answers3

2

Your are running a console program in Windows directly from the IDE. Windows opens a terminal window for the duration of the program and closes it immediately upon program termination.

This problem has been plaguing students trying to lean C on this legacy system for 25 years. Microsoft seems to be unwilling to fix this issue, either in the terminal or in the IDE. Truth is they do not encourage C programming at all, but the same problem occurs for other languages too.

You can force the program to wait for extra input before terminating, so you can see its output. Yet you must read more than one extra character as scanf() will have left the newline produced by the key pending in the standard input.

Here is a modified version that should work:

#include <stdio.h>

int main() {
    int year;

    printf("Enter year below\n");

    if (scanf("%d", &year) != 1) {
        printf("invalid input\n");
    } else {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0)
                    printf("%d is a Leap year\n", year);
                else
                    printf("%d is not a Leap year\n", year);
            } else {
                printf("%d is a Leap year\n", year);
            }
        } else {
           printf("%d is not a Leap year\n", year);
        }
    }
    getchar();  /* read the pending newline */
    getchar();  /* read extra input to keep the terminal window open */
    return 0;
}

As chux commented, the algorithm implements the rules for the Gregorian Calendar established by pope Gregory XIII in 1582, and adopted progressively at different times across the world. Using this method for years before 1582 seems incorrect from a historical perspective, but is actually quite common and useful. It is known as the Proleptic Gregorian Calendar.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
1

It should be scanf("%d", &year); instead of scanf("%d", year);. scanf requires the address of the variable to write into. Your compiler should have warned you about that.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

If you're directly running the .exe from Windows, it'll automatically close because once the application finishes, Windows closes the window. You can run it from the command prompt or put something at the end of your code to hold the session open (like getchar()).

yhyrcanus
  • 3,743
  • 2
  • 23
  • 28