Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Trouble in SImple C Program

Tags:

c

scanf

I'm having problem with a simple C program. Even if I enter a year between 1000 and 1999 it still displays invalid year. Please tell me what's happening??

#include <stdio.h>

main()
{
    int year;
c:
    printf("\n\nEnter a Year: ");
    scanf("%d", year);

    if ((year < 1000) || (year > 1999))
    {
        printf("\n\nInvalid Year");
        goto c;
    }
    convert(year);

}
convert(int year)
{
    printf("%d", year);
}
like image 587
shr3jn Avatar asked Feb 03 '26 15:02

shr3jn


1 Answers

You need to pass an address to scanf, i.e.:

scanf("%d", &year);

Note the ampersand.

like image 101
Adam Zalcman Avatar answered Feb 05 '26 05:02

Adam Zalcman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!