# Working with Arrays in C: Variable-Length Arrays

This section discusses a feature in the language that enables you to work with arrays in your programs without having to give them a constant size.

In the examples in this chapter, you have seen how the size of an array is declared to be of a specific size. The C language allows you to declare arrays of a variable size. For example, Program 7.3 only calculates the first 15 Fibonacci numbers. But what if you want to calculate 100 or even 500 Fibonacci numbers? Or, what if you want to have the user specify the number of Fibonacci numbers to generate? Study Program 7.8 to see one method for resolving this problem.

Program 7.8   Generating Fibonacci  Numbers  Using Variable-Length Arrays

// Generate Fibonacci numbers using variable length arrays

#include <stdio.h>

int main (void)

{

int i, numFibs;

printf (“How many Fibonacci numbers do you want (between 1 and 75)? “);

scanf (“%i”, &numFibs);

if (numFibs < 1 || numFibs > 75) {

return 1;

}

unsigned long long int Fibonacci[numFibs];

Fibonacci = 0;       // by definition

Fibonacci = 1;       // ditto

for ( i = 2; i < numFibs; ++i )

Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1];

for ( i = 0; i < numFibs; ++i )

printf (“%llu “, Fibonacci[i]);

printf (“\n”);

return 0;

}

Program 7.8   Output

How many Fibonacci numbers do you want (between 1 and 75)? 50

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584

4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229

832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817

39088169 63245986 102334155 165580141 267914296 433494437 701408733

1134903170 1836311903 2971215073 4807526976 7778742049

Program 7.8 has several points worth discussing. First, the variables i and numFibs are declared. The latter variable is used to store the requested number of Fibonacci numbers that the user wants to generate. Notice the range of the entered value is checked by the program, which is good programming practice. If the value is out of range (that is, less than 1 or greater than 75), the program displays a message and returns a value of 1. Executing the return statement at that point in the program causes the program to ter- minate immediately, and no further statements are executed. As noted in Chapter 3, “Compiling and Running Your First Program,” the nonzero value that is returned indi- cates by convention that the program terminated with an error condition, and that fact could be tested by another program if desired.

After the number has been entered by the user, you see the statement

unsigned long long int Fibonacci[numFibs];

The Fibonacci array is declared to contain numFibs elements. This is called a variable length array because the size of the array is specified by a variable and not by a constant expression. Also, as previously  noted, a variable can be declared anywhere in a program, as long as the declaration occurs before the variable is first used. So although this decla- ration appears out of place, it’s perfectly legitimate.  It’s not usually considered good programming style to do this, however, mainly because, by convention, the variable declarations are often grouped together so someone reading the program can see the variables and their types in one place.

Because Fibonacci numbers get large very quickly, the array is declared to contain the largest positive integer value you can specify, namely an unsigned long long int. As an exercise, you might want to determine the largest Fibonacci number that you can store inside an unsigned long long int variable on your computer.

The rest of the program is self-explanatory: The requested number of Fibonacci num- bers are calculated and then displayed to the user. The execution of the program is then complete.

A technique known as dynamic memory allocation is also often used to allocate space for arrays while a program is executing. This involves using functions such as malloc and calloc that are in the standard C library. This topic is discussed in detail in Chapter 17, “Miscellaneous and Advanced Features.”

You have seen how arrays are powerful constructs that are available in virtually all programming languages. A program example showing the use of multidimensional arrays is deferred to Chapter 8, which begins a detailed discussion of one of the most impor- tant concepts in the C language—the program function. Before proceeding to that chap- ter, however, try to work the following exercises.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.