Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "&&" and "and"? [duplicate]

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

C Code:

#include <stdio.h>

int gcd(int x, int y);

int main()
{
    int m,n,temp;
    printf("Enter two integers: \n");
    scanf("%d%d",&m,&n);
    printf("GCD of %d & %d is = %d",m,n,gcd(m,n));
    return 0;  
}

int gcd(int x, int y)
{
    int i,j,temp1,temp2;

    for(i =1; i <= (x<y ? x:y); i++)
    {
        temp1 = x%i;
        temp2 = y%i;
        if(temp1 ==0 and temp2 == 0)
            j = i;
    } 
    return j;         
}

In the if statement, note the logical operator. It is and not && (by mistake). The code works without any warning or error.

Is there an and operator in C? I am using orwellDev-C++ 5.4.2 (in c99 mode).

like image 886
haccks Avatar asked Jun 28 '13 12:06

haccks


People also ask

Are or is there any difference?

If it's a countable plural noun, then you use “are”. “Are there any socks in the drawer?” If it's an uncountable noun, you would use “is”. “Is there any water left?” If it's a singular countable noun, you would use “is”.

Is there any differences between it and this?

The main difference between it and this is that it is a third person singular personal pronoun whereas this is a demonstrative adjective and pronoun. The functions and meaning of these two words also change due to the differences in their grammatical categories.

Which is correct difference in or difference between?

Consider these two ways of saying the same thing: "There's a difference in the way you use 'cold' and 'frigid'," versus "There's a difference between 'cold' and 'frigid'." So use "in" with a single object, and "between" with a pair of objects.


8 Answers

&& 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>

like image 117
Shafik Yaghmour Avatar answered Oct 17 '22 12:10

Shafik Yaghmour


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.

like image 42
chollida Avatar answered Oct 17 '22 12:10

chollida


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    &&
like image 34
Pierre Fourgeaud Avatar answered Oct 17 '22 11:10

Pierre Fourgeaud


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

like image 43
Thijser Avatar answered Oct 17 '22 13:10

Thijser


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.

like image 29
Keith Thompson Avatar answered Oct 17 '22 12:10

Keith Thompson


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.

like image 27
Chinna Avatar answered Oct 17 '22 13:10

Chinna


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.

like image 39
P0W Avatar answered Oct 17 '22 13:10

P0W


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

like image 31
faradaj Avatar answered Oct 17 '22 13:10

faradaj