Reputation: 9
#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf("%c",name);
printf("Enter your birth year : ");
scanf("%i",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
I am trying to take the value of birth_year as an input but it automatically assigns it to 0 for some reason what am I doing wrong
Upvotes: 0
Views: 78
Reputation: 31
In the first scanf you should read a string instead of a char, that should do it. Also, it's always good to have a whitespace before you read a char, so it resets the buffer memory.
#include <stdio.h>
int main()
{
char name[10];
int birth_year;
printf("Enter your name : ");
scanf(" %s",name);
printf("Enter your birth year : ");
scanf(" %d",&birth_year);
int age = 2020 - birth_year;
printf("Your age is %i",age);
}
Upvotes: 2