Steve Allen, 78, the zany, cerebral and vastly prolific comedian, actor and writer who hosted television's original "Tonight" show, composed an estimated 4,000 songs and wrote 40 books on topics ranging from show business to the state of the world, died of an apparent heart attack Oct. 30 at the home of his son in Encino, Calif.

"He said he was a little tired after dinner," said Bill Allen. "He went to relax, peacefully, and never reawakened."

Mr. Allen became host of the "Tonight" show when it was introduced in 1953 on WNBT, an NBC station in New York. It moved to the network in 1954. He was such a success that NBC scheduled "The Steve Allen Show" to run opposite "The Ed Sullivan Show" on CBS beginning in 1956.

At that point, Mr. Allen cut back his appearances on the nightly show to three a week, and he left it altogether at the end of the 1956 season. He was replaced by Ernie Kovacs in 1957 in a new format called "Tonight! America After Dark." That show failed, and NBC went back to "Tonight," with Jack Paar as host. Johnny Carson took over in 1962.

But Mr. Allen was credited with establishing virtually all of the conventions of late-night television, through Carson to Jay Leno and David Letterman--the opening monologue, chatting with the bandleader and relying on a lineup of regular characters.

In the last category, Mr. Allen was famous for his rendition of "The Question Man," a routine in which someone would give him an answer and he would think up the question. It was a forerunner of Carson's Carnac the Magnificent character. In another routine, Mr. Allen would read rock-and-roll lyrics as if they were great poetry. He also read letters to the editor in the Daily News.

"The Steve Allen Show" was a variation on the "Tonight" show and featured a man in the street interview, still a staple on the Letterman program. Comics who were introduced on the show included Louis ("Hi-ho, Steverino!") Nye, Don Knotts, Tom Poston, Pat Harrington and Bill Dana. In one memorable episode, Elvis Presley appeared in a comedy routine and in another the Three Stooges followed Lenny Bruce as guests.

The program stayed on NBC through 1960 and then was carried by ABC for a year before closing.

"It was tremendous fun to sit there night after night reading questions from the audience and trying to think up funny answers to them," Mr. Allen told an interviewer. "Reading angry letters to the editor; introducing the greats of comedy, jazz, Broadway and Hollywood; welcoming new comedians like Shelley Berman, Jonathan Winters, Mort Sahl and Don Adams."

Although his contribution to the "Tonight" show is part of television history, Mr. Allen thought his best work was in "Meeting of the Minds," which appeared on PBS from 1977 to 1981 and on which actors played such historical figures as Cleopatra, Attila the Hun and Sigmund Freud and discussed their ideas.

Mr. Allen was capable of departing from the usual television fare. He once devoted an entire "Tonight" show to organized crime. On another occasion, he drank six vodkas in the course of a show to demonstrate what happens to people who try to drive after drinking.

His skill with ad lib jokes was evident from an incident early in his career when he was a disc jockey and broke in to tell listeners he had "the final score for you on the big game between Harvard and William and Mary. It is: Harvard 14, William 12, Mary 6."

Mr. Allen's film credits included the title role in the 1955 movie "The Benny Goodman Story." He also appeared on Broadway in "The Pink Elephant" in 1953 and in nightclubs and on soap operas. He wrote newspaper columns and commented on wrestling matches.

His output of songs was so great that he was listed in Guinness World Records as the most prolific songwriter of modern time. The best known of them was "This Could Be the Start of Something Big." His books included "Mark It and Strike It," which was his autobiography, and "Morality and Nuclear War," which appeared in 1961.

Mr. Allen set strict standards for himself and those around him. He "was a perfectionist," said Ron DeFore, who was associate director of "The Allen Show," a syndicated 90-minute talk show Mr. Allen hosted in the early 1970s. "But you could never fault him for it because he wound up turning out excellence."

DeFore, who lives in Northern Virginia, said that after taping three 90-minute shows in succession, Mr. Allen would send the staff a 10-page memo of intricately detailed suggestions for improvements in such details as who was shown on what camera at what moment. "All of us did respect him for it," DeFore said, "because he was usually right."

Mr. Allen was born in New York on Dec. 26, 1921. His parents were Billy Allen and Belle Montrose Allen, both vaudevillians. He attended Drake University on a scholarship but dropped out to become a disc jockey in Phoenix. During World War II, he served briefly in the Army.

Discharged in 1943 because of asthma, he returned to the radio station in Phoenix. He soon moved to Los Angeles, where he did a midnight show on KNX radio. This gained him a small but enthusiastic audience. In 1950, his program was carried on the CBS network as a summer replacement for "Our Miss Brooks." He was soon invited to New York for "The Steve Allen Show," which was carried five nights a week.

In politics, Mr. Allen described himself as an advocate of "radical middle-of-the-roadism." He spoke out on such topics as capital punishment and freedom of expression. In his later years, he became a critic of the increasing sexual content on television. In a speech last year, he said talk shows had taken the medium "to a garbage dump."

Mr. Allen's marriage to Dorothy Goodman ended in divorce. They had three sons, Steve Jr., David and Brian. In 1954, he married actress and comedienne Jayne Meadows, with whom he had Bill Allen.

In a recent biographical note, Mr. Allen said he thought of television as "junk food of the mind."

"Like junk food of the stomach," he continued, "it's not very harmful in itself. It's just that it's something to pass the time. This was brought to my mind during a visit to a veterans hospital during the Vietnam War. Here were these kids in those damn beds--some of them are probably still there--and what a godsend television is to people in that kind of predicament."

Staff writer Martin Weil contributed to this report.