22

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).

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
haccks
  • 104,019
  • 25
  • 176
  • 264

8 Answers8

23

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

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 5
    Well, the other difference is that when you write `and`, most readers will get confused. Especially if you write it as in e.g. `int a, b, and c = a;` – Potatoswatter Aug 12 '13 at 12:01
  • 6
    I would not agree that it is confusing for "most readers". It is only confusing for those who know C and C++ enough to be used to seeing `&&` but do not know about alternative tokens (or iso646.h in the case of C). It is not confusing to those who do know about alternative tokens (rare) or those who are starting out (I would argue that it would actually be *less* confusing for the latter, and probably largest by numbers, group) – Joe Aug 12 '13 at 12:12
  • @Joe Can someone tell, without otherwise knowing about alternative tokens, that `and` has short-circuit behavior and the RHS won't be evaluated if the LHS is `false`? And what about other, non-operator uses as in my above example? – Potatoswatter Aug 12 '13 at 12:13
  • 2
    @Potatoswatter The first point is a non-argument as it applies equally to `&&` for people who are not experienced in C++ (for whom `and` has the advantage of actually stating what it means). Non operator uses in rvalue references are another matter (though your example isn't valid) and should of course be avoided. – Joe Aug 12 '13 at 12:17
  • @Joe What about people the majority of people who aren't completely inexperienced? C++ isn't a language to just throw at the clueless. My example is absolutely valid; it's an rvalue reference. – Potatoswatter Aug 12 '13 at 12:27
  • 3
    @Potatoswatter I don't think those actually *are* the majority. And for those with experience in other languages, there are plenty with short circuiting logic operators with spelled out names (and, or, not), for example: Python (which many praise for using those instead of &&, ironically not knowing that C++ supports and, or, and not as well) – Joe Aug 12 '13 at 12:34
  • I am amazed that clearly-readable plain English alternatives exist and that people still prefer the cryptic punctuation versions instead. I wonder how many bugs have been created by people swapping `&` with `&&` – endolith Apr 04 '15 at 00:47
10

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.

chollida
  • 7,834
  • 11
  • 55
  • 85
8

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    &&
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
7

Basically and is just the text version of && in . 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

chollida
  • 7,834
  • 11
  • 55
  • 85
Thijser
  • 2,625
  • 1
  • 36
  • 71
  • note the link you provided is for a C++ compiler and not a C compiler as the poster has indicated. – chollida Jun 28 '13 at 13:09
  • I know however it still holds true.C++ and C are often very similar and this deals with one of these similarities. – Thijser Jun 28 '13 at 13:11
  • @Thijser; Thanks for the link. Your answer lacks only in explaining about macro, because on changing extension `.c` to `.cpp` it works as it includes macro it self. +1. – haccks Jun 28 '13 at 13:40
  • 1
    In C++ it's not really a macro, it's a language feature (and `#include ` does nothing.) – aschepler Jun 28 '13 at 14:25
4

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
3

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.

Chinna
  • 3,930
  • 4
  • 25
  • 55
3

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.

P0W
  • 46,614
  • 9
  • 72
  • 119
2

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

faradaj
  • 3,629
  • 1
  • 22
  • 21