An overriding sense of trouble -- a foreboding that Turkey may be heading into insurmountable difficulties -- is gripping this strategically located country that borders on the Soviet Union and anchors the southern flank of NATO.

It is an atmosphere fed by an unemployment rate of 20 percent, an annual inflation rate running at 60 percent, a surge of politically linked violence that has claimed about 1,000 lives in the past year, industrial output that has dropped to 50 percent of capacity, and an economy that is virtually bankrupt from foreign debts and unable to find new financing.

The events in neighboring Iran have added to the general sense here that a period of political turmoil is brewing for Turkey.

The troubles in Turkey -- a country of about 42 million people -- are causing rising concern within NATO, especially in West Germany, which has sought to focus alliance attention on the situation. In the United States, the specter looms of another ally in trouble. In addition, the loss of valuable U.S. electronic listening posts in Iran used to monitor Soviet missile testing has heightened interest in the future of similar stations near the Soviet border here.

Many Turkish political, business and intellectual leaders retain a degree of optimism about the long-term future of Turkey. Yet there is "a brooding fear," according to Prof. Aydin Yalcin, a liberal former member of parliament, based on "a combination of bad luck, foreign lack of understanding and sympathy and widespread unrest."

The lights in major cities go out for two hours every day due to an energy shortage that reflects lack of foreign currency to pay for oil imports and failure to complete many power stations started in the early 1970s.

Public services are declining. In this capital city, not far from the tourist shopping areas, garbage piles up in the streets uncollected. Buses are jammed because there is no foreign currency to pay for new ones. There are shortages of oil, margarine, light bulbs and even Turkey's famous coffee.

Six trains have crashed in recent weeks, killing 47 people. The crashes, although unrelated to the other difficulties, have contributed to the feeling of many Turks that something is wrong.

Writing this week in the Istanbul newspaper Hurriyet, the nation's largest daily, columnist Oktay Eksi said, "Degeneration is like this: first you think nothing will happen. Everything will go on as before. Then you see things bursting here and there, cracks, spillings, the rot setting in."

After an outbreak of religious warfare between rival Islamic sects in Maras in December which took 102 lives, Turkey's liberal Prime Minister Bulent Ecevit declared martial law in 13 provinces, including this capital and Istanbul.

It is a low-key martial law, very little in evidence here in the capital. Yet Turkey now joins nearby Iran and Pakistan as countries under martial law and threatened by internal upheaval. All of them, along with the United States, are partners in the Central Treaty Organization, a pact at one time aimed at maintaining stability in the important region.

Unlike Iran, Turkey has been a parliamentary democracy for about 30 years. There are outlets here through political parties -- including extremist parties on the left and right and a sizable Islamic religious party -- for all points of view. There is an elected government and not an authoritarian figure such as the shah of Iran to rebel against.

While the Turks are Moslems, this is a secular state with a history of modernization inaugurated by Ataturk in the 1920s. It is generally viewed as an unlikely place for some new expression of Islamic fundamentalism to assert itself as the predominant force as it did in Iran.

"We have no Khomeini here," says a Turkish student, referring to the aged religious leader whom the Iranians have rallied behind.

Yet there is always the possibility, a Turkish businessman adds, that the religious issue could be used by extremists on both sides to topple the government if the political violence continues.

The National Salvation Party, which represents the Islamic revivalist movement here, actually lost half its strength between the 1973 and 1977 elections, falling to 24 seats from 48 in the 450-seat National Assembly and dropping to 8.5 percent of the vote.

The party's leader, Necmettin Erbakan, seems to have been discredited politically here. But his number two man and chief party rival, Korkut Ozal, is viewed as a more formidable leader, able to capitalize on popular discontent if he can wrestle the leadership away.

The significance here of Iran's upheaval is that it is perceived as adding to the idea, as an Ankara woman put it, "that this is a time of turmoil and it's all around us."

It has also added to the Turkish establishment's fear that eventually the Communists will undermine the religious revivalists in Iran and take over there, putting more pressure on Turkey.

Ironically, several Western diplomats here observe, the turmoil in Iran may wind up "saving" Turkey because there is now intensive Western focus on events here and an awareness of how fast countries once regarded as stable allies can go under.

Although Turkey -- whose 450,000-man ground army is NATO's largest in Europe -- has always been a trusted member of NATO, it is by far the poorest within a well-to-do alliance. It is the only member with a non-Christian, eastern culture.

In a sense, NATO's concerns are chickens coming home to roost. Turkey has always been treated within the alliance in strategic and military terms, with little attention paid to economic needs. The notion that it pays to support democracies, especially poor ones, somehow was soft-pedaled.

The economic aid was never much of a problem because the Turkish economy was small. But the growth here in the early 1970s was substantial, as has been the collapse after the 1974 oil crisis and recession. Now, Turkey's staggering economic needs are seen as the key coping with the rest of the nation's predicaments.

What is needed, sources here say, is a mainly Western international effort to bail Turkey out -- with insistence on reforms to restore bank confidence and eventually help Turkey support itself. The tab for the next year alone could run to $1.5 billion, representing the difference between Turkey's earning abroad and the minimum amount needed to keep industry running so the domestic situation does not become even more a volatile.

Turkey's high population growth and modernazition have produced a massive movement to the cities, exacerbating problems that might already have brought revolt to less stable countries.

Historical mistrust of the neighboring Soviets remains widespread here but Ecevit has moved toward better relations. He also has sought closer ties with Arab and nonaligned states.

This has been interpreted by some in the West as a dangerous drift toward neutralism and away from NATO. Ecevit rejects this, saying his country will remain in NATO. It is also clear, his aides say, that the East does not have the money or skills to help Turkey.

Nevertheless, Ecevit says that security also means looking after regional relationships. There is a sense here that Turkey has been left out of detente and the developing relations with the Communist East.

Some of the current policy here can be traced to bitterness over the long U.S. arms embargo on Turkey imposed by Congress after the 1974 Turkish invasion of Cyprus.

"Under the arms embargo, you couldn't be pro-American, says Prof. Yalcin, "when your ally denies your army what it needs. Even I advocated trying a course sympathetic to the Soviet Union and Third World. There was no alternative. Otherwise we would have been too exposed. It was natural, not ideological."

Today, many Turks say attitudes toward the United States have improved following President Carter's successful effort to get the embargo lifted. Turks and Americans here agree, however, that the former trust is gone. In a subtle yet important way, that has contributed to the overall sense of unease.

Politically, the center still controls Turkish politics and it remains generally moderate and pro-Western. The disappointment with Ecevit is that he failed to make necessary economic reforms and curb the violence immediately after his election victory a year ago.

Ecevit rules by a tiny majority and there is enormous bitterness, if not hatred, between him and opposition leader Suleiman Demirel. This means it would be hard to form a "national survival" coalition if things do come apart, even though there is talk of just such a potential political solution.

In the meantime, the fringe parties grow more extreme and violent.

On the extreme right, the National Action Party under Alpaslan Turkes has doubled its share of the national vote to 3.5 percent and 16 parliamentary seats. It is also frightening a lot of Turks.

The party has a strong nationalistic, authoritarian streak and somewhat racist appeal that has generated charges of neofascism among some of its opponents. There is widespread speculation that youthful party supporters are involved in political assassinations and the acts of terrorism that have plagued the country.

The extreme left is also said to be increasingly violent. It is split into rival Maoist and Leninist factions. University sources estimate that 5 to 15 percent of the student body and faculty at Turkish universities is Marxist.

An Ankara journalist pointed out that the right and left fringe movements, while antagonistic, press parallel themes -- that democracy, capitalism, Western orientation and NATO are no good for Turkey. Part of this is also endorsed by the Islamic Salvation Party.

"So this is a real danger," said Prof. Yalcin. "These are strong forces. Turkey may be in in the stage of Italy in the 1920s or Germany in the 1930s. If Turkey is left alone politically, militarily and economically like Germany was in the 1930s, with unemployment, industrial failures and pessimism spread through the country, a right- or left-wing demagogue might capture the imagination of a hopeless people.

"This is my fear."

The question remains, said an experienced Western diplomat here. "How much economic chaos and deprivation can a developing democratic society take and still remain democratic? Turkey is one of the few nations of the world that can truly feed itself. It has lots of resources to be developed and hard-working people. But it is hard to know what the reserves of tolerance are here.

"A year ago," he said, "I would not have guessed they could have handled what they have today."