“&&” and “and” operator in C

前端 未结 8 1840
名媛妹妹
名媛妹妹 2020-11-30 10:05

I am trying to calculate the Greatest Common Denominator of two integers.

C Code:

#include 

int gcd(int x, int y);

int main()
{
             


        
相关标签:
8条回答
  • 2020-11-30 10:15

    It is compiling to you because I think you included iso646.h(ciso646.h) header file. According to it and is identical to &&. If you don't include that it gives compiler error.

    0 讨论(0)
  • 2020-11-30 10:16

    and is just an alternative token for &&.

    We can easily quote the standard here :

    2.6 Alternative tokens [lex.digraph]

    In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling. The set of alternative tokens is defined in Table 2.

    In table 2 :

       Alternative | Primary
            and    |    &&
    

    But I suggest you to use &&. People used to C/C++ may get confused by and...


    Since it is merged now, we are talking also about C, you can check this page ciso646 defining the alternatives tokens.

    This header defines 11 macro constants with alternative spellings for those C++ operators not supported by the ISO646 standard character set.

    From the C99 draft standard :

    7.9 Alternative spellings <iso646.h>

    The header defines the following eleven macros (on the left) that expand to the corresponding tokens (on the right):

       and    &&
    
    0 讨论(0)
  • 2020-11-30 10:22

    The and operator is the text equivalent of && Ref- AND Operator

    The or operator is the text equivalent of || Ref.- OR Operator

    So resA and resB are identical.

    0 讨论(0)
  • 2020-11-30 10:26

    Check out the page here iso646.h

    This header defines 11 macro's that are the text equivalents of some common operators. and is one of the defines.

    Note that I can only test this for a C++ compiler so I'm not certain if you can use this with a strict C compiler.

    EDIT I've just tested it with a C compiler here and it does work.

    0 讨论(0)
  • 2020-11-30 10:28

    If the code in your question compiles without errors, either you're not really compiling in C99 mode or (less likely) your compiler is buggy. Or the code is incomplete, and there's a #include <iso646.h> that you haven't shown us.

    Most likely you're actually invoking your compiler in C++ mode. To test this, try adding a declaration like:

    int class;
    

    A C compiler will accept this; a C++ compiler will reject it as a syntax error, since class is a keyword. (This may be a bit more reliable than testing the __cplusplus macro; a misconfigured development system could conceivably invoke a C++ compiler with the preprocessor in C mode.)

    In C99, the header <iso646.h> defines 11 macros that provide alternative spellings for certain operators. One of these is

    #define and &&
    

    So you can write

    if(temp1 ==0 and temp2 == 0)
    

    in C only if you have a #include <iso646.h>; otherwise it's a syntax error.

    <iso646.h> was added to the language by the 1995 amendment to the 1990 ISO C standard, so you don't even need a C99-compliant compiler to use it.

    In C++, the header is unnecessary; the same tokens defined as macros by C's <iso646.h> are built-in alternative spellings. (They're defined in the same section of the C++ standard, 2.6 [lex.digraph], as the digraphs, but a footnote clarifies that the term "digraph" doesn't apply to lexical keywords like and.) As the C++ standard says:

    In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling.

    You could use #include <ciso646> in a C++ program, but there's no point in doing so (though it will affect the behavior of #ifdef and).

    I actually wouldn't advise using the alternative tokens, either in C or in C++, unless you really need to (say, in the very rare case where you're on a system where you can't easily enter the & character). Though they're more readable to non-programmers, they're likely to be less readable to someone with a decent knowledge of the C and/or C++ language -- as demonstrated by the fact that you had to ask this question.

    0 讨论(0)
  • 2020-11-30 10:28

    && and and are synonyms and mean Logical AND in C++. For more info check Logical Operators in C++ and Operator Synonyms in C++.

    0 讨论(0)
提交回复
热议问题