Home > Backend Development > C#.Net Tutorial > Is go a keyword in C language?

Is go a keyword in C language?

青灯夜游
Release: 2023-01-12 17:27:27
Original
4777 people have browsed it

go is not a keyword in C language, goto is a keyword in C language. The goto statement is called an unconditional transfer statement, which allows unconditional transfer of control to a labeled statement within the same function; the syntax is "goto label;...label: statement;", where label can be any pure statement other than the C keyword Text, which can be set before or after the goto statement in a C program.

Is go a keyword in C language?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

There are a total of 32 keywords in C language:

##gotodowhileforcontinuestructshortunsigned
1 2 3 4 5 6 7 8
if ##else ##switch case default break return
typedef ##enumunion char
int long float double void sizeof signed
const autoregister static extern volatile

It can be seen that go is not a keyword in C language, goto is a keyword in C language.

c language goto statement

The goto statement is also called an unconditional transfer statement in C. It is said to be unconditional, but in fact it is still conditional. Jump The transfer scope is still limited, allowing unconditional transfer of control to marked statements within the same function.

Grammar

The syntax of goto statement in C language:

goto label;
..
.
label: statement;
Copy after login

Here, label can be any plain text except C keyword, It can be set before or after the goto statement in the C program.

Is go a keyword in C language?

Flowchart

Is go a keyword in C language?

Usage

First, we need to determine Well, where we want to transfer, that is, the transfer end point, we need to set an identifier, that is, label (I am not showing English, when the goto statement makes an error, this word is likely to appear in the error content), and at the same time , we need to add a colon (:) after the label. In this way, we have set the identifier, and then we set the starting point - the goto identifier;. First we have to tell the computer that we are going to jump, which is the role of goto. Then we have to tell it where we want to jump, which is the identifier we just set. Finally, like other statements, we add points to it. (;) means the end of the statement. Here's what the editor page looks like.

Is go a keyword in C language?

and the running results

Is go a keyword in C language?

It can be seen that our printf in line 5 is not executed, and it goes directly to line 7 printf.

It can jump to the next statement or jump to the front.

Is go a keyword in C language?

This way it becomes a simple loop with no end condition

Is go a keyword in C language?

You can try running this For a piece of code, of course we can use a counter and add if judgment, give it an end condition, and let it have the ability of for and while.

Unfortunately, we cannot use goto across functions. There are other statements that can be implemented, so we will not go into details here.

Is go a keyword in C language?

Here, if the if condition is true, after we output it, looking at the long string of uneven brackets below, I know what I want to do They set conditions there, and then break over and over again. It's annoying to think about it. How can I wait without goto at this time? Very soon! We directly goto and jump to

Is go a keyword in C language?

to proceed to the next stage of our program.

But I wonder if you noticed that in the previous picture, we used two gotos, but they pointed to the same label. Yes, we can make the program jump at different starting points. to the same end point, but it should be noted that we cannot jump to two labels under the same goto. This is easy to understand. If it were you, receiving an order asking you to go to two or even more different places, You will also be confused, where should I go?

Another difference from before is that I added a semicolon (;) after the identifier xiayige:. Why is this? The identifier must be followed by a statement, which can be an assignment or a statement. But if there is really no way to add a statement after it, we can add a semicolon and treat it as an empty statement to make the identifier run.

Ps: In fact, you can declare a garbage variable here, or other meaningless statements, but it will make the code difficult to understand, and it is not recommended to do so.

This is what everyone does most with goto. As for other uses, it depends on your imagination. As the old saying goes, how bold a person is, how productive the land is.

The following is when I use goto.

#include<stdio.h>
int main ()
{
    printf("请输入要计算的算式,四则运算优先级一样高,从左到右依次计算\n");
    int jieguo=0,sz,gongju=0,gongju2=1;
    char ysf;
    scanf("%d",&jieguo);
    if (jieguo==0)
//直接输入等号的话%d似乎是0,有待商榷!!!!!!!!
//二次修改,if语句中判断量时只有0为假,除此之外的数字都表真
    {
    printf("**,你算**呢\n");
    goto chaojijieshu;
    gongju=1;
    gongju2=0;
    }
    else if (gongju2)
    
    {
    printf("请输入运算符\n");
    
    scanf(" %c",&ysf);
    //enter包含两个命令,算是两个字符\r和\n,后面的一个会占据scanf的输入位,所以要清空,或者用空格占位
    if(ysf==&#39;=&#39;)
    printf("**,你算**呢\n");
    }
loop:
    while(ysf!=&#39;=&#39;)
    {
        if (ysf==&#39;-&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo-=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
        else if (ysf==&#39;+&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo+=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
        else if(ysf==&#39;*&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            jieguo*=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }        }
        else if(ysf==&#39;/&#39;)
        {
            printf("请输入数字\n");
            scanf("%d",&sz);
            while(sz==0)
            {
                printf("0能做分母吗?你算**呢\n");
            }
            jieguo/=sz;
            printf("请输入运算符\n");
            fflush(stdin);//清空标准输入流(stdin) fflush是stdio.h中的函数
            scanf("%c",&ysf);
            if(ysf==&#39;=&#39;)
            {
                goto jieshu;
            }
            else
            {
                goto loop;
            }
        }
    }
jieshu:
    printf("结果是%d\n",jieguo);
chaojijieshu:
    if (gongju)
    printf("真无语,重开吧\n");
    return 0;
    
}
//注意注意!!!!!!!!!!!!
//enter算是两个命令,所以有两个字符,在进行上一次输入之后
//在来一个enter,会占据scanf的字符位
//所以要清空标准输入流
//可以利用下述语句
//fflush(stdin);
//清空标准输入流(stdin) fflush是stdio.h中的函数
//以上是最好的解决办法
//初次之外,还有被称作偏方的办法
//二次修改 除此之外,而不是初次之外,打错字了
//如
//scanf(“ %c”,&ysf”)
//在scanf中加一个空格,可以顶掉enter多出来的一个字符
//还有
//加一句
//getchar()
//用getchar来捕捉多出来的\n
Copy after login

Related recommendations: "C Video Tutorial"

The above is the detailed content of Is go a keyword in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template