Why is my float printed as 0, although I enter 13.5 for it using scanf?

I find it hard to understand why my float variable continues to print 0 when I entered it as a number.

code:

int num, month, day, year; float price[10000]; 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; 

I entered 555 for my product number, 13.5 for my price and 10/24/2010 for my date. When I did this, he printed that my price was 0.00 rubles. He does this for any number that I entered. Why?

+6
source share
4 answers

Just change this:

 float price[10000]; 

:

 float price; 

Since you are using it as a single variable and not as an array

+2
source

You cannot insert an array value like this -

 scanf("%f", &price); 

Use a for loop to insert values ​​into the array price -

 for(i=0; i<sizeWhatYouWant; i++){ scanf("%f", &price[i]); } 

Or just change the declaration of float price[10000] to -

 float price; 
+4
source

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/* [10000] it doesn't need to be an array */; 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 .

+2
source

Two things can be noticed.

  • change the float price[10000]; at float price; , since you will use only one float variable, so you do not need an array.

  • You need to check the return value of scanf() to ensure correct input.

In addition, as a note, you may want to initialize local variables, since they are not automatically initialized.

+2
source

Source: https://habr.com/ru/post/981684/


All Articles