Walter Matthau, 79, a comic actor who specialized in outrageous, irascible and frequently irresistible characters in films such as "The Fortune Cookie," which won him an Oscar, and "The Odd Couple," a latter-day Hollywood classic about two guys sharing an apartment, died of a heart attack Saturday at St. John's Health Center in Santa Monica, Calif.

A pencil-thin figure who stood 6 feet 3 inches tall, Mr. Matthau had a gravelly voice, a notably expressive face and a flawless sense of timing. He was a classically trained actor who appeared in a wide range of roles from drama to farce in the course of a career that spanned more than a half-century.

He made his Broadway debut in 1948 playing an 83-year-old English bishop in "Anne of a Thousand Days," which starred Rex Harrison. He began his film career in 1955 playing a villain in "The Kentuckian," which starred Burt Lancaster. In 1969, he appeared in "Hello Dolly!" with Barbra Streisand, with whom he publicly feuded, and in 1976, he played a bleary-eyed, beer-swilling Little League baseball coach in "The Bad News Bears." In 1981, he played a U.S. Supreme Court justice in "First Monday in October."

His last film--"Hanging Up," with Meg Ryan, Diane Keaton and Lisa Kudrow--was released in February.

He was best known, however, for his work with Jack Lemmon in "The Fortune Cookie" in 1966, "The Odd Couple" in 1968 and a number of later films. In "The Fortune Cookie," he played Lemmon's brother-in-law, a two-bit lawyer, the role for which he received the Academy Award for best supporting actor.

In "The Odd Couple," based on the Neil Simon play, he played Oscar Madison, a splenetic, permanently disheveled and recently divorced sportswriter. Lemmon played Felix Unger, a fastidious, pin-neat and recently divorced photographer.

Mr. Matthau had appeared in the highly successful Broadway production of the play in 1965. The film version made him a Hollywood star.

"Every actor looks all his life for a part that will combine his talents with his personality," Mr. Matthau told Time magazine in 1971. " 'The Odd Couple' was mine. That was the plutonium I needed. It all started happening after that."

Mr. Matthau and Lemmon later appeared together in "The Front Page," "Buddy Buddy," "Grumpy Old Men," "Grumpier Old Men" and "Out to Sea." Close friends in real life, they always appeared as adversaries on the screen. In 1998, they reprised their most famous roles together in "Odd Couple II."

In 1971, Mr. Matthau was nominated for an Academy Award for best actor for his role as the cantankerous old man in "Kotch," which was directed by Lemmon, and again in 1975 for his role as George Burns's vaudeville partner in "The Sunshine Boys."

In all, Mr. Matthau appeared in nearly 60 films. His early efforts included "King Creole," which starred Elvis Presley; "A Face in the Crowd," with Andy Griffith; "Lonely Are the Brave," with Kirk Douglas; and "Charade," which starred Cary Grant and Audrey Hepburn. He also appeared in action thrillers such as "The Taking of Pelham One Two Three" and "The Laughing Policeman."

Other films included "Cactus Flower," "Plaza Suite," "Pete 'n' Tilly," "Casey's Shadow," "California Suite," "Little Miss Marker," "I Ought to Be in Pictures," "Pirates" and "Dennis the Menace."

Mr. Matthau's television credits included appearances in dramatic productions on "Playhouse 90" and "DuPont Show of the Week." He also starred in "Tallahassee 7000," an unsuccessful series.

Although he made his reputation doing comedy, he never wanted to be known as a comedian.

"When people come up to me and say, 'Aren't you that comedian who's in the movies?' I want to throw up," he once told an interviewer. "I throw up a lot."

In fact, Mr. Matthau could be both funny and fanciful, and his subjects included the facts of his own life. Applying for a Social Security number in 1937, he claimed his middle name was Foghorn, and he never bothered to change it.

He also joked about his height. He used to say that by the time he was 10 years old he was already 6 feet tall: "When I drank a cherry soda, I looked like a thermometer."

When he filled out a form for his entry in Current Biography, he said his father had been an Eastern Orthodox priest in czarist Russia who ran afoul of church authorities by preaching the infallibility of the pope. In fact, his father was a Jewish peddler in Kiev who deserted his family when the future actor was 3.

Mr. Matthau was born in New York. His name was Walter Matuschanskayasky. Walter and his older brother, Henry, grew up on the Lower East Side of Manhattan. Their mother was a garment worker.

Walter showed an early interest in the theater. He began reciting Shakespeare at age 7, and he appeared in school assemblies. By the time he was 11, he was selling soft drinks in the city's Yiddish theaters and making occasional onstage appearances.

But when he graduated from high school, he began a series of government jobs. He was a forester in Montana, a gym instructor for the Work Projects Administration and a police boxing coach. In World War II, he served in the Army Air Forces as a radio operator and cryptographer with a heavy bomber group in Europe.

When the war ended, the GI Bill gave him an opportunity to get into the theater. He enrolled in the dramatic workshop of New York's New School, where his fellow students included Gene Saks, Rod Steiger, Harry Guardino and Tony Curtis. He stayed three years, appearing in regional theaters during the summer.

He liked to tell the story of how he got his break in "Anne of a Thousand Days." Although he was an understudy, he had never rehearsed his part. When he went on stage, Rex Harrison gave him a startled look and uttered an expletive.

According to Mr. Matthau, people in the audience began muttering, "Did he say,'Oh, spit?' "

Mr. Matthau subsequently appeared in a number of unsuccessful Broadway productions until he got a part in the hit "Will Success Spoil Rock Hunter?" He also was a success in "Once More with Feeling" and "A Shot in the Dark." His next stop was Hollywood.

Throughout his life, Mr. Matthau had a reputation as a gambler, and he never discouraged the stories about his betting. He once claimed his losses at $5 million.

Over the years, he had several serious health problems, including a heart attack in 1966, heart bypass surgery in 1976 and pneumonia in the 1990s. He attributed these to his eating habits.

"If you eat only celery and lettuce, you won't get sick," he used to say. "I like celery and lettuce, but I like it with pickles, relish, corned beef, potatoes, peas. And I like Eskimo Pies, vanilla ice cream with chocolate covering."

Mr. Matthau was married to Grace Geraldine Johnson from 1948 until 1958, when they divorced. He is survived by his wife Carol Marcus; their son, Charles; two children from his previous marriage, David and Jenny. Charles, a filmmaker, directed his father in "The Grass Harp."