“&&” and “and” operator in C

前端 未结 8 1841
名媛妹妹
名媛妹妹 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:35

    && and and are alternate tokens and are functionally same, from section 2.6 Alternative tokens from the C++ draft standard:

    Alternative Primary
        and       &&
    

    Is one of the entries in the Table 2 - Alternative tokens and it says in subsection 2:

    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.

    As Potatoswatter points out, using and will most likely confuse most people, so it is probably better to stick with &&.

    Important to note that in Visual Studio is not complaint in C++ and apparently does not plan to be.

    Edit

    I am adding a C specific answer since this was originally an answer to a C++ question but was merged I am adding the relevant quote from the C99 draft standard which is section 7.9 Alternative spellings <iso646.h> paragraph 1 says:

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

    and includes this line as well as several others:

    and    &&
    

    We can also find a good reference here.

    Update

    Looking at your latest code update, I am not sure that you are really compiling in C mode, the release notes for OrwellDev 5.4.2 say it is using GCC 4.7.2. I can not get this to build in either gcc-4.7 nor gcc-4.8 using -x c to put into C language mode, see the live code here. Although if you comment the gcc line and use g++ it builds ok. It also builds ok under gcc if you uncomment #include <iso646.h>

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

    Basically and is just the text version of && in c. You do however need to #include <iso646.h>. or it isn't going to compile.

    You can read more here:
    http://msdn.microsoft.com/en-us/library/c6s3h5a7%28v=vs.80%29.aspx

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