You declared price as an array that you need to make in this way
int num, month, day, year; float price[10000]; printf("Enter item number: \n"); scanf("%d", &num); printf("Enter unit price: \n"); scanf("%f", &price[0]); /* <---- it not &price it &price[0] */ printf("Enter purchase date (mm/dd/yyyy): \n"); scanf("%d/%d/%d", &month, &day, &year); printf("Item\t\tUnit\t\tPurchase\n"); printf(" \t\tPrice\t\tDate\n"); printf("%d ", num); printf("$%.2f ", price[0]); /* <---- it not price it price[0] */ printf(" %d/%d/%d\n", month, day, year); return 0;
you need to store the value in the first element of the array, and then also print the first element, i.e. price[0] .
If you just want to read a single value, you do not need to declare price as an array, so this will be the solution
int num, month, day, year; float price; printf("Enter item number: \n"); scanf("%d", &num); printf("Enter unit price: \n"); scanf("%f", &price); printf("Enter purchase date (mm/dd/yyyy): \n"); scanf("%d/%d/%d", &month, &day, &year); printf("Item\t\tUnit\t\tPurchase\n"); printf(" \t\tPrice\t\tDate\n"); printf("%d ", num); printf("$%.2f ", price); printf(" %d/%d/%d\n", month, day, year); return 0;
You printed the address into the first element of the array instead of the value at that address, and it converted to unsigned int or unsigned long int , so when you used the specifier "%f" , it was printing 0 .
In both cases :
To prevent this error, you should enable warnings from your compilers if you use gcc
gcc -Wall -Wextra -Werror ${SOURCE_FILES} -o ${OUTPUT_FILE}
will do it.
In addition, if the input is invalid, your program will have undefined behavior, you need to check that scanf() really read the value that you gave it to read, which is achieved by checking the return value from scanf() which is equal to the number of matching elements in your case
if (scanf("%d", num) != 1) errorInputWasInvalid();
since you are requesting element 1 , then scanf() should return 1 .