c - Printing an int value obtained from user -
the problem @ age
part, compiler not give me errors when run it prints random number
int age
printf("enter name:"); scanf(" %s",&name1); int age; printf("\n\nhow old you?"); scanf(" %d",&age); char gender; printf("\n\nenter gender[male/female]:"); scanf(" %s",&gender); char confirmation; printf("confirmation: name %s , %d years old , , %s.\n\nanswer[y/n]:",&name1,age,&gender);
here problem.
char gender; scanf(" %s",&gender);
gender
char
. is, has memory 1 byte character. using string. have same problem name1
since using &
can't sure don't show that.
change like:
char gender[8] // enough fit "female" , terminating null scanf("%7s", gender);
extra note: scanf
bit awkward use prevent buffer safety. may consider fgets
sscanf
instead.
Comments
Post a Comment