0

I am trying to execute a program that prints the numerical value when the && operator returns true and when it returns false. The code is as follows:-

#include <stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Part I\n");
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%3 == 0) && (b%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%5 == 0) && (b%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%7 == 0) && (b%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
printf("Part II\n");
printf("The AND operator yields: %d\n",(a%2 == 0) && (b%2 == 0));
printf("The AND operator yields: %d\n",(a%3 == 0) && (b%3 == 0));
printf("The AND operator yields: %d\n",(a%5 == 0) && (b%5 == 0));
printf("The AND operator yields: %d\n",(a%7 == 0) && (b%7 == 0));

return 0;
}

The output ( along with my input ) is as follows:-

210
210
Part I
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
Part II
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1

Why is the first part behaving in such a manner? This is happening even when I replace && by ||. I am using a Borland C++ Compiler 5.5 . Please Help.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
kusur
  • 598
  • 11
  • 33
  • 5
    Please, for the sake of everyone, use a compiler that was written in the last decade. Borland C++ is ancient. You will run into a ton of weird bugs and missing features if you use it. –  Jan 12 '13 at 20:34
  • agreed with @duskwuff better use `gcc` coz your code working fine on `gcc` – exexzian Jan 12 '13 at 20:36
  • 1
    @sansix: But the behaviour is undefined. – Oliver Charlesworth Jan 12 '13 at 20:40
  • @OliCharlesworth yeah it is which varies from one compiler to another – exexzian Jan 12 '13 at 20:55
  • @duskwuff Of course there are newer versions and one should consider adapting to C99/C11. But I believe the Borland 5.5 complier was released about 10 years ago. If one is only concerned with having a C90 compliant compiler, there's a lot of worse ones out there. Particularly bad ones are Visual Studio or gcc without the -std option, those are far far worse than Borland 5.5. gcc -std=c99 -pedantic-errors is a very good compiler, however. – Lundin Jan 12 '13 at 21:43
  • I'd propose a close-duplicate, but fat chance of that, so [see this document](http://stackoverflow.com/questions/1860159/how-to-escape-the-sign-in-cs-printf) – WhozCraig Jan 12 '13 at 22:31

3 Answers3

4

Because if you want to actually display a %, then you must escape it in the printf format string with another %. e.g.

printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
          ^              ^
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • So two % signs are nullifying the effect of %d as the integer format specifier. Hence, the sentence is getting printed as it is. But then the problem arises that the two % symbols aren't adjacent to the last one, so how is this happening? – kusur Jan 12 '13 at 20:34
  • @kusur: In your original code, the behaviour was **undefined**. What you were seeing was an artifact of your particular compiler; with a different compiler, something different might have happened. – Oliver Charlesworth Jan 12 '13 at 20:35
0

I've tested this with http://codepad.org/, which I think uses gcc, and the code worked ok. But you might try to add an extra % before a literal % (i.e, %%) so the compiler knows the % that follows is an actual character. Like this:

printf("Part I\n");
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%%3 == 0) && (b%%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%%5 == 0) && (b%%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%%7 == 0) && (b%%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
Alko
  • 672
  • 10
  • 21
0

You are actually using illegal escape sequence character to print % in the first part. Thats why printf is yielding garbage values.

printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
              ^                        ^
      Here is you are mistaking

It should be like

printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));  

You can also read about all format specifiers used in C.

Pankaj Prakash
  • 2,300
  • 30
  • 31