You need to define array size to tell the compiler how much space to allocate:
char U1[256];
If you don't know the size of your array at compile time, you can dynamically allocate memory using malloc
:
// #include <stdlib.h>
int *arr;
int n, i;
printf("Number of elements: ");
scanf("%d", &n);
// Allocate n ints
arr = malloc(n * sizeof(int));
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Here they are: ");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
// Free the array on the end!
free(arr);
Note
printf("The %s has %i bytes.", U1, sizeof(U1));
will always print 256, as sizeof
returns the size of the array deducted at compile time, not the number of the characters just read into the array. You could use sizeof(char) * (strlen(U1) + 1)
to compute number of bytes required by string (+1 comes from NUL-terminator character at the end of the string).