Spain's electorate, by a decisive majority, endorsed the country's membership in NATO today in a surprising vote that apparently reflected strong, last-minute warnings by Socialist Prime Minister Felipe Gonzalez about the consequences of a withdrawal from the alliance.

Deputy Prime Minister Alfonso Guerra said that with 99 percent of the vote counted, 52.5 percent favored staying in NATO and 39.8 percent voted to pull out. Blank or voided ballots made up the other 7.7 percent.

Interior Minister Jose Barrionuevo forecast that the final result would show 8.8 million voters favoring continued membership in the North Atlantic Treaty Organization to 7.1 million against and about 1.2 million blank or void votes.

The referendum, the first held by any country on the issue of the Atlantic Alliance, had been monitored closely by NATO allies. A senior western diplomat in Madrid said before the voting that he feared a Spanish "no" to NATO could encourage European pacifist and disarmament movements.

Western allies acclaimed the outcome of the referendum, which represented a clear political victory for Gonzalez. He had battled against what polls indicated was an adverse public opinion to keep Spain within the alliance.

The results were greeted with relief by NATO Secretary General Lord Carrington and other alliance officials, who had feared Spanish withdrawal as a serious blow to NATO unity and a propaganda victory for the Soviets. "I know I reflect the feelings of all other members in welcoming the results," Carrington said.

White House deputy press secretary Edward Djerejian called the results "fantastic," and the State Department said that Spain's decision has "reinforced the sense of confidence shared by the members of the western democratic community. We welcome this result."

West German Foreign Minister Hans-Dietrich Genscher called today a "great day for the western alliance" because of the vote, according to spokesman Reinhard Betzuege.

Gonzalez said in a televised broadcast tonight that the results had been "a clear victory" and "a triumph for the Spanish people." He said Spain would now participate actively within Europe and the West for peace and security.

Barrionuevo said that 60 percent of Spain's 29 million registered voters had cast ballots. No violence was reported to have taken place during the polling.

In the final days of the plebiscite campaign, Gonzalez had claimed that Spain would face "political instability" and "economic uncertainty" if the nation voted against the alliance.

Last Thursday, when the final opinion polls were published (Spanish law bans polls during the week before an election), voter surveys in the Spanish press showed "no" voters in the lead by a margin of four to 10 points.

Spain's Communist Party leader, Gerardo Iglesias, who had campaigned against NATO membership, charged that Gonzalez and the Socialist government won through "threats and blackmail" and he accused the prime minister of manipulating public opinion.

While the campaign in favor of NATO withdrawal was led by the Communist Party, it attracted considerable support from Socialist voters as well as from conservatives anxious to register their opposition to the Socialist premier.

The decision by many Socialists to vote against NATO membership reflected Gonzalez's original hostility, as opposition leader, to Spain's entry into the alliance under a center-right government in 1982.

During campaigning for the general election that swept him to power in 1982, Gonzalez promised to put the issue of NATO to a referendum. Although, once in power, he changed his position on NATO and became supportive of the alliance, the Socialist leader stayed by his electoral pledge to hold a NATO referendum.

Gonzalez's campaign to keep Spain in NATO faced the additional hurdle of a boycott by the center-right Coalicion Popular, the major opposition group to the Socialist government. Although they favor the Atlantic Alliance, Coalicion Popular leaders had called on their supporters to abstain in the referendum, arguing that it was unnecessary and that they disagreed with the terms set by Gonzalez for continued membership in NATO.

Ballots in today's plebiscite listed the terms as nonnuclearization, nonintegration in NATO's military command and a reduction in U.S. troop presence in Spain.

Voters were asked whether they considered it in the interest of Spain to remain in the Atlantic Alliance under such conditions.

The terms were set by Gonzalez with the aim of reducing the level of opposition to the alliance. Coalicion Popular leaders, in calling for abstention, said that Gonzalez was seeking a personal plebiscite on his own government's record and that they opposed the Socialist government's decision to remain outside NATO's military command structure.

Soon after gaining power in 1982, Gonzalez's administration froze negotiations to join NATO's military wing and decided on a French-style political link with the alliance.

The provisional results showed that the conservative abstention call had made an impact on the result. Voter participation was 20 points down from the 1982 general election and seven down from a referendum in 1978 that ratified Spain's democratic constitution.

Gonzalez appeared to have kept the majority of the Socialist voters with him and to have attracted also a number of conservative voters who preferred to put NATO membership first and party politics second.