To declare a float array in C, you can use array notation or allocate memory dynamically.
In this example,
floatArray
with predefined values using array notation.#include <stdio.h>
int main() {
float floatArray[] = {1.2, 3.4, 5.6};
int size = sizeof(floatArray) / sizeof(floatArray[0]);
printf("Float Array: ");
for (int i = 0; i < size; ++i) {
printf("%.1f ", floatArray[i]);
}
printf("\n");
return 0;
}
Float Array: 1.2 3.4 5.6
In this example,
emptyFloatArray
by allocating memory dynamically using malloc()
function.#include <stdio.h>
#include <stdlib.h>
int main() {
int size = 3;
float* emptyFloatArray = (float*) malloc(size * sizeof(float));
printf("Empty Float Array: ");
for (int i = 0; i < size; ++i) {
printf("%.1f ", emptyFloatArray[i]);
}
printf("\n");
free(emptyFloatArray);
return 0;
}
Empty Float Array: 0.0 0.0 0.0
In this tutorial, we learned How to Declare a Float Array in C language with well detailed examples.