Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use goto sentences in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use goto sentences in C language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

A brief introduction

C language provides goto statements and tag jump tags that can be misused at will.

In theory, goto statements are not necessary, but in practice, it is easy to write code without goto statements.

However, goto statements are still useful in some situations, and the most common use is to terminate the processing of some deeply nested structure of the program, such as jumping out of two or more layers of loops at a time.

The use of break in this situation will not achieve the purpose. It can only exit from the innermost loop to the upper loop.

Grammar

The syntax of goto statements in C language:

Goto label;...label: statement; example comparison # includeint main () {int c = 1; if (c) {goto start;} start: printf ("instance 1\ n"); printf ("instance 2\ n"); printf ("instance 3\ n"); printf ("instance 4\ n") Printf ("instance 5\ n");}

Output result:

# includeint main () {int c = 1; if (c) {goto start;} printf ("instance 1\ n"); printf ("instance 2\ n"); printf ("instance 3\ n"); start: printf ("instance 4\ n"); printf ("instance 5\ n");}

Output result:

Here is an example of using the goto statement:

Shutdown program # include int main () {char input [10] = {0}; system ("shutdown-s-t 60"); again: printf ("the computer will shut down within 1 minute, if you enter: I am a pig, cancel the shutdown!\ nPlease enter: >"); scanf ("% s", input); if (0 = = strcmp (input, "I am a pig") {system ("shutdown-a");} else {goto again } return 0;}

If the goto statement is not applicable, you can use a loop:

# include # include int main () {char input [10] = {0}; system ("shutdown-s-t 60"); while (1) {printf ("computer will be shut down within 1 minute, if you enter: I am a pig, cancel shutdown!\ nPlease enter: >"); scanf ("% s", input); if (0 = = strcmp (input, "I am a pig") {system ("shutdown-a"); } return 0;}

The scenarios where the goto language is really suitable are as follows:

For (...) For (...) {for (...) {if (disaster) goto error;}}... Error:if (disaster) / / handle error condition

It can be used instead of multiple break jumps here.

The above is the editor for you to share how to use the C language goto sentence, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report