Got problems in scanf() program
Hi, me agin.
Here is my program, my first name gets mixed up when the program prints it out, why?
Also, the book said that you dont need to put & at variables when you use %s(strings) in scanf, but it looks like you do have to put & all the time.
Here is my program, my first name gets mixed up when the program prints it out, why?
Code:
/* This is a sample program that asks users for some basics data and
prints it on screen in order to show what was entered */
#include <stdio.h>
main()
{
//Set up the variables that scanf will fill
char firstInitial;
char lastInitial;
int age;
int favorite_number;
printf("What is your first name?\n");
scanf(" %s", &firstInitial);
printf("What is your last name?\n");
scanf(" %s", &lastInitial);
printf("How old are you?\n");
scanf(" %d", &age);
printf("What is your favorite number (integer only)?\n");
scanf(" %d", &favorite_number);
printf("\nYour intitials are %s.%s. and you are %d years old", &firstInitial, &lastInitial, age);
printf("\nYour favorite number is %d.\n\n", favorite_number);
return 0;
}
Also, the book said that you dont need to put & at variables when you use %s(strings) in scanf, but it looks like you do have to put & all the time.
