精华内容
下载资源
问答
  • C语言花括号

    千次阅读 2014-09-15 15:30:00
    实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组成一个结构上可以认为是一个语句的复合语句。 所以在程序中,我们应该把复合语句看成是单条语句,而不是多条语句,这种效果也可以用来区分...
    由于C语言本身就是函数式语言,说白了,C程序就是由函数构成的!
    所以花括号肯定是用在函数之中,包括函数中的各种流程控制语句中。
    
    实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组成一个结构上可以认为是一个语句的复合语句。
    
    所以在程序中,我们应该把复合语句看成是单条语句,而不是多条语句,这种效果也可以用来区分程序的层次结构。所以花括号都是成对出现的。否则,程序就会报错!
    
    至于你说if语句为什么加花括号,那要看if条件后面的语句情况了,如果是多条语句,逻辑上是一个整体,都要执行的话,就必须要用花括号括起来,表示是一个整体,是一个复合语句;如果只有一条语句,那花括号就可以省略不用了,当然加上也没有任何问题,也是正确的!

    转载于:https://www.cnblogs.com/zhouhbing/p/3972921.html

    展开全文
  • C语言花括号检查器

    2019-07-07 11:25:02
    我们从大一开始学C语言,都知道在C语言花括号是需要配对使用的,左括号和右括号也是有规定的。现在请你来设计一个程序用来检查你写的程序中花括号是不是使用正确。 Input 输入由若干段字符组成,每一段由5个“”...

    我们从大一开始学C语言,都知道在C语言中花括号是需要配对使用的,左括号和右括号也是有规定的。现在请你来设计一个程序用来检查你写的程序中花括号是不是使用正确。

    Input
    输入由若干段字符组成,每一段由5个“”作为结尾;每一段中包含符合C语言规定的字符;若干段加起来总长度不超过10000个字符。为了处理方便,我们规定一旦遇到5个连续的“”,那么就是段落分界,这5个“”不是段落内容。输入保证每个段落有5个连续的“”作为结尾。如果最后一次连续5个“*”之后还有其他字符,就视为无效字符,不需要处理。

    Output
    如果检查出花括号使用不对,那么输出一行“Error !”;如果使用正确,那么输出花括号最深层次的内容(不包括花括号本身),如果最深层次的内容为空,则输出一行“Blank !”,如果有多组层次相同,那么按照在原程序中的先后次序输出所有组的内容。

    Sample Input

    #include<iostream>
    using namespace std;
    int main()
    {
    	return 0;
    }
    *****
    #include<iostream>
    using namespace std;
    int main()
    {
    }
    *****
    #include<iostream>
    using namespace std;
    int main()
    }
    {
    *****
    #include<iostream>
    using namespace std;
    int main()
    *****
    #include<iostream>
    using namespace std;
    int main()
    {}
    *****
    

    Sample Output
    这里的输出显示有误,请看hint.

    Hint
    第一段的输出有空行,也有tab键等内容;第二段的输出是3行空行,因为本身就有回车换行符。样例输出:

    return 0;

    Error !
    Blank !
    Blank !

    展开全文
  • #define max(x,y) ({ \  typeof(x) _x = (x); \  typeof(y) _y = (y);...在这个宏中,花括号里表达式的值为最后一条语句的值,然后用小括号将大括号括起来就可以给其他变量赋值了。当红语句中的最外层小括号
    #define max(x,y) ({ \
        typeof(x) _x = (x); \
        typeof(y) _y = (y); \
        (void) (&_x == &_y); \

        _x > _y ? _x : _y; })


    在这个宏中,花括号里表达式的值为最后一条语句的值,然后用小括号将大括号括起来就可以给其他变量赋值了。当红语句中的最外层小括号不用的话,也就是如果这个宏语句改为如下时:

    #define max(x,y)  { \
        typeof(x) _x = (x); \
        typeof(y) _y = (y); \
        (void) (&_x == &_y); \

        _x > _y ? _x : _y; }

    就会出现错误。具体为什么会出现这样的情况本人也不是很清楚。但在网上看到一个这样的介绍:

    gcc的扩展用法,标准C++不支持。

    它相当于是扩展了primary-expression
       primary-expression:
         identifier
         constant
         string-literal
         ( expression )

       GNU extensions:

       primary-expression:
         __func__
           (treated as a keyword in GNU C)
         __FUNCTION__
         __PRETTY_FUNCTION__
         ( compound-statement )         // compound-statement 就是花括号语句
         __builtin_va_arg ( assignment-expression , type-name )
         __builtin_offsetof ( type-name , offsetof-member-designator )
         __builtin_choose_expr ( assignment-expression ,
                                 assignment-expression ,
                                 assignment-expression )
         __builtin_types_compatible_p ( type-name , type-name )
    6 Extensions to the C Language Family
    GNU C provides several language features not found in ISO standard C. (The ‘-pedantic’
    option directs GCC to print a warning message if any of these features is used.) To test for
    the availability of these features in conditional compilation, check for a predefined macro
    __GNUC__, which is always defined under GCC.
    These extensions are available in C and Objective-C. Most of them are also available in
    C++. See Chapter 7 [Extensions to the C++ Language], page 581, for extensions that apply
    only to C++.
    Some features that are in ISO C99 but not C90 or C++ are also, as extensions, accepted
    by GCC in C90 mode and in C++.

    6.1 Statements and Declarations in Expressions
    A compound statement enclosed in parentheses may appear as an expression in GNU C.
    This allows you to use loops, switches, and local variables within an expression.
    Recall that a compound statement is a sequence of statements surrounded by braces; in
    this construct, parentheses go around the braces. For example:
    ({ int y = foo (); int z;
    if (y > 0) z = y;
    else z = - y;
    z; })
    is a valid (though slightly more complex than necessary) expression for the absolute value
    of foo ().
    The last thing in the compound statement should be an expression followed by a semi-
    colon; the value of this subexpression serves as the value of the entire construct. (If you use
    some other kind of statement last within the braces, the construct has type void, and thus
    effectively no value.)
    This feature is especially useful in making macro definitions “safe” (so that they evaluate
    each operand exactly once). For example, the “maximum” function is commonly defined
    as a macro in standard C as follows:
    #define max(a,b) ((a) > (b) ? (a) : (b))
    But this definition computes either a or b twice, with bad results if the operand has side
    effects. In GNU C, if you know the type of the operands (here taken as int), you can define
    the macro safely as follows:
    #define maxint(a,b) \
    ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
    Embedded statements are not allowed in constant expressions, such as the value of an
    enumeration constant, the width of a bit-field, or the initial value of a static variable.
    If you don’t know the type of the operand, you can still do this, but you must use typeof
    (see Section 6.6 [Typeof ], page 296).
    In G++, the result value of a statement expression undergoes array and function pointer
    decay, and is returned by value to the enclosing expression. For instance, if A is a class,
    then290 Using the GNU Compiler Collection (GCC)
    A a;
    ({a;}).Foo ()
    will construct a temporary A object to hold the result of the statement expression, and that
    will be used to invoke Foo. Therefore the this pointer observed by Foo will not be the
    address of a.
    Any temporaries created within a statement within a statement expression will be de-
    stroyed at the statement’s end. This makes statement expressions inside macros slightly
    different from function calls. In the latter case temporaries introduced during argument
    evaluation will be destroyed at the end of the statement that includes the function call. In
    the statement expression case they will be destroyed during the statement expression. For
    instance,
    #define macro(a) ({__typeof__(a) b = (a); b + 3; })
    template<typename T> T function(T a) { T b = a; return b + 3; }
    void foo ()
    {
    macro (X ());
    function (X ());
    }
    will have different places where temporaries are destroyed. For the macro case, the tem-
    porary X will be destroyed just after the initialization of b. In the function case that
    temporary will be destroyed when the function returns.
    These considerations mean that it is probably a bad idea to use statement-expressions of
    this form in header files that are designed to work with C++. (Note that some versions of
    the GNU C Library contained header files using statement-expression that lead to precisely
    this bug.)
    Jumping into a statement expression with goto or using a switch statement outside the
    statement expression with a case or default label inside the statement expression is not
    permitted. Jumping into a statement expression with a computed goto (see Section 6.3
    [Labels as Values], page 291) yields undefined behavior. Jumping out of a statement ex-
    pression is permitted, but if the statement expression is part of a larger expression then
    it is unspecified which other subexpressions of that expression have been evaluated except
    where the language definition requires certain subexpressions to be evaluated before or after
    the statement expression. In any case, as with a function call the evaluation of a statement
    expression is not interleaved with the evaluation of other parts of the containing expression.
    For example,
    foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
    will call foo and bar1 and will not call baz but may or may not call bar2. If bar2 is called,
    it will be called after foo and before bar1
    展开全文
  • C语言 判断花括号是否成对出现

    千次阅读 2015-10-24 16:50:24
    C语言 判断花括号是否成对出现

    编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现。
    例如:
    输入:{}{},成功匹配。
    输入:{}{{}},成功匹配
    输入:{}}{,匹配不成功
    输入:}}{{,匹配不成功
    输入:{}},匹配不成功
    输入:}{}{{,匹配不成功

    #include <stdio.h>
    int main()
    {
        int ch = 0;
        int count = 0;
        while ((ch = getchar()) != EOF)
        {
            if (ch == '}' && count == 0)
            {
                printf("not matched\n");
                system("pause");
                return 0;
            }
            else if (ch == '{')
            {
                count++;
            }
            else if (ch == '}' && count != 0)
            {
                count--;
            }
        }
        if (count == 0)
        {
            printf("mathched\n");
        }
        else
        {
            printf("not matched\n");
        }
        system("pause");
        return 0;
    }
    

    这里写图片描述

    按Ctrl+z 后回车键退出

    展开全文
  • C语言花括号配对问题

    千次阅读 2017-03-31 22:48:12
    //花括号配对 #include int main() { char ch; int a=0; while((ch=getchar())!= EOF) { if(ch =='{') a ++; else if(ch =='}'&& a==0) { printf("不匹配\n"); return 0; } else if(ch =='}'&& a!=0) a--;
  • <p><img alt="" height="976" src="https://img-ask.csdnimg.cn/upload/1605227289425.png" width="1920" /></p> 求教该如何纠正 <p> </p>
  • C语言::验证花括号成对出现

    千次阅读 2016-07-07 14:15:54
    算法总结 判断左右花括号是否成对...具体分析,请看C语言::验证花括号成对出现 基础版 #include int main(){ //定义一个整型变量用来接收控制台数据 int ch = 0; //定义一个整型变量用来计算左花括号数目 int le
  • C语言】验证花括号成对出现

    千次阅读 2016-07-07 22:03:22
    编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现。 注意:你不必担心注释内部、字符串常量内部和字符常量形式的花括号。 算法分析 我们先判断左花括号的数量是否相等。 如果左右...
  • 像(Statements and Declarations in Expressions)这类的复合语句,我更愿意理解为是一个表达式,即小括号加上复合型语句变成一个表达式,整体({})执行的过程为先执行复合语句里面的每个表达式直到最后一个,然后...
  • 标签:c#define_CRT_SECURE_NO_WARNINGS#include#includeintmain(){inti=0,a=0;intj=0,b=0;intcount=0;chararr1[50];chararr2[20];printf("请输入源代码:\n");scanf("%s",arr1);for(i=0;i{if(arr1[...
  • 从标准输入获取C语言代码并判断花括号是否匹配。 代码如下,仅供参考: #include int main() { static int i=0; static int j=0; char c; while((c=getchar())!=EOF) { if(c=='{') { i++; } if(c=='}'&&...
  • #define _CRT_SECURE_NO_WARNINGS #include #include int main() { int i = 0, a = 0; int j = 0, b = 0; int count = 0; char arr1[50]; char arr2[20]; printf("请输入源代码:... for (i 
  • // 编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。//报错不循环,循环时不判断,有问题#include int main() { int a,n=0,m=0; printf("请输入源代码:\n"); a=getchar(); if ( a...
  • 花括号是成对出现的,遗漏花括号会改变程序的运行方式。一般来讲,人们更容易忘记右边作为程序结束的花括号。目前要记住:主程序以“{”号开始,“}”号结束。记住下面的格式是有益处的。 vo...
  • //判断括号 函数的定义 #include "括号配对.h" void InitStack(LinkStack *top)//将链栈初始化 { if((*top = (LinkStack)malloc(sizeof(LStackNode)))== NULL) { exit(-1); } (*top)->next = NULL; } int StackEmpty...
  • #includeint brace, brack, paren;void in_quote(int c);void in_comment(void);void search(int c);main(){int c;extern int brace, brack, paren; //声明全局变量while((c = getchar()) != EOF){if(c == '/'){if(...
  • #include int main() { int i,j=0;  char s[100];  printf("输入字符:");  scanf("%s",&s);    for(i=0;s[i];i++)  {   if(s[i]=='}') { if(j==0) { printf("匹配不成功");... }
  • 版本一:#include <stdio.h>#define MAXLINE 1000 /* max input line size */ char line[MAXLINE]; /*current input line*/int getline(void); /* taken from the KnR book. */ int main() { int len=0;...
  • 练习 1-24 编写一个程序,查找C语言程序中的基本语法错误,如圆括号、方括号、花括号不配对等。要正确处理引号(包括单引号和双引号)、转义字符序列与注释。 代码块: #include &amp;amp;lt;stdio.h&amp;...
  • 圆括号、方括号、花括号是否匹配 单双引号、转义字符序列与注释是否符合语法规定 状态变量state state是用来标志当前读入字符是在什么位置,比如是否在单行、多行注释内、单双引号内或者不在前述内容中。取值如下:...
  • ” , 也就是说,当有花括号 {} 结尾可不需要分号, 当然你加了不也会出错,但不要while(); 【总结】在C语言中,用;和{}的作用都是一个:用于语句分隔,告诉编译器当语句遇到;或者{}中的 “}”时,说明执行该语句...
  • 此题的解决思路为:利用计数的方法判断花括号是否正确成对出现,遇到左花括号count++,遇到右花括号count--,若结果输出为0,则花括号成对出现。 但此时需要注意,若在计算的过程中count=0时遇到右花括号则没有正确...
  • 花括号

    2011-08-29 20:40:15
    花括号为了把一些语句或代码打个包包起来,使之形成一个整体,并与外界绝缘。 char a[10] = {“abcde”}; char a[10] { = “abcde”};//错误。 花括号里面的内容没有形成一个整体。>>>摘至《C语言深度解剖》2.4...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 901
精华内容 360
关键字:

c语言花括号

c语言 订阅