Home > front end >  Leap year program in C
Leap year program in C

Time:02-26

I am just starting to code in C and as a part of a tutorial assignment, I was given to write a program to take input and check if it's a leap year or not. Very basic. I pretty much wrote the code and ran it but during debugging I realized my 'year' variable was storing some garbage value. I searched on the web quite extensively and found out that my code is not wrong. But I'm surely wrong as I'm getting garbage values but not able to find out where I made a mistake. Here is the code.

#include <stdio.h>
#include <conio.h>
void main(){
    int year;
    printf("Enter any year \n");
    scanf("%d", &year); //take input
    printf("%d", &year);    //just to debug
     if((year%4==0) && ((year0!=0)||(year@0==0)))  //check the input for leap year
        printf("\nIt's a Leap Year");
    else
        printf("\nIt's not a Leap Year");
}

I am using VSCode as my IDE.

The sample output is

Enter any year
2016
767555836
It's a Leap Year

Please help me figure out what I am missing here. Thank you in advance.

CodePudding user response:

printf specifier '%d' take the value of the variable. not the address of variable.when you use & before the name of the variable its mean the address of variable. but you want to print the value of the variable. so dont use &.

printf("%d", year);

it should work now.

  •  Tags:  
  • c
  • Related