Atop once-elegant buildings downtown, families have installed themselves in former storage huts and servants' rooms. And in the posh neighborhoods of Zemalek and Heliopolis, old high-rises are sprouting additional stories - mostly illegal - as landlords cash in on the housing crunch.

Rooftop overcrowding was depicted somewhat quaintly in a popular Egyptian film released last year called ''The Roofs.'' In the film a colorful assortment of families and schemers lives together on one roof. In the open space outside their rooms, they knead pancake dough together, roast melon seeds, watch television, and sip tea while their sons play a ferocious game of soccer.

Indeed, the rooftop offers some advantages over the alternatives that poor and lower-middle-class Egyptians can afford.

''It's a more natural life away from all the noise and auto exhaust,'' says Gamal, a young man who moved to a downtown rooftop when his father became the building's concierge. Gamal's family of eight shares the roof with two other families, along with dogs, chickens, a pigeon house, and before Muslim feasts, a few lambs.

On one recent night, Gamal and this reporter attended a memorial service for the relative of a friend, Ibrahim Khairy, who lives on a roof nearby. Friends of the family filed up the fire escape - the only access - as an elderly sheikh filled the evening air with Koranic chanting. A dozen men sat in chairs around him, while women prepared tea and babies wailed in the kitchen.

The Khairys occupy five of the tiny rooms that line the edges of their roof. They share three outhouses with another family. Ibrahim, now the manager of a small hotel, says he liked being waked by the sun and remembers having room to play outdoors as a child - something he cherished in a city that now offers each inhabitant just 11 square centimeters of green space, according to government figures.

Whatever their virtues, rooftop accommodations are usually substandard in the government's view, often occupied without permits and deficient in plumbing, heating, and other necessities. In addition, roof dwellers usually lack legal security.

But officials are more concerned with curtailing another type of informal housing: squatting in the mauso-leums and burial plots in Cairo's vast City of the Dead.

''It's normal to find people living on the roofs,'' concedes an official in the Ministry of Development. ''Cairo is a city planned to accommodate 2 million which now has 9 million.'' He had no estimates of the size of the city's rooftop population.

The government's determination to stem construction on the fertile soil at the city's periphery is another reason officials are not preoccupied with rooftop settlements. Vertical growth, at least, does not encroach on Egypt's shrinking agricultural land mass. The skyward trend is likely to persist, since few expect the government's costly solution to the housing shortage - the construction of cities and suburbs in the desert - to meet the demand for affordable shelter.

Cairo's better neighborhoods have been experiencing their own brand of rooftop squatting. Incongruous, sometimes massive additions are being heaped onto existing buildings, often without engineering studies or government approval. These skyward expansions are contributing to building collapses that have killed hundreds of Cairenes since 1980. Last year, one such addition in Heliopolis brought a building crashing down on the villa of a deputy minister of tourism, killing him and 17 others.

The Development Ministry official blames the extent of unlicensed construction on a variety of factors. These include the red tape and delays encountered along the legal route, and understaffing and corruption in the ministry's enforcement branch. He says these problems will decrease under Egypt's newly installed Cabinet and with the establishment of a new housing police.

It seems doubtful, however, that more inspectors and construction in the desert will bring a halt to the improvised solutions some Cairenes have found to the housing crisis. As the city's population soars toward the 13 million figure the United Nations expects it to reach by the turn of the century, at least some of that increase seems destined to occur overhead. But rooftop dwellers say their choice is preferable to a tiny, decrepit apartment, a mausoleum tract, or a shack at the city's edge.

''Unless I had a big house in the country,'' Gamal reflects, ''this is where I want to live.''