1

I came across a question in the book and it asked me to write the output of the following program.

#include<stdio.h>

int main()
{
     int j=4;
     ( !j != 1 ? printf("\nWelcome") : printf("GooD Bye"));
     return 0;
}

I am not able to understand basically how the Welcome is printed on running the program. Can anyone please explain with the help of hierarchy of operators and what values the compiler calculates according to the expression?

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Pranav Jituri
  • 823
  • 1
  • 11
  • 25
  • First look at !j. The ! inverts TRUE / FALSE values. In this case, TRUE is anything non-zero (which 4 is), and FALSE is 0. So !j is the same as !4 which evaluates to FALSE (or 0). 0 != 1 evaluates to TRUE, so the first part of the shorthand if/else is executed, which is the "Welcome" portion. – Steven Hansen Dec 19 '13 at 18:27
  • The logical NOT operator, !, will convert non-zero integers to zero, and zero to some non-zero, usually 1, but don't be surprised if -1 pops up. This is a quandary that Forth faced during standardization. Is the logical NOT of all zero bits, all one bits, or a single one bit? I don't think the ANS Forth committee could ever work it out, and NOT was left undefined in the spec (sad that). – Brian Tiffin Dec 19 '13 at 18:30
  • @BrianTiffin - So I might sometime encounter a case as well where the Logical Not Operator might return (-1) ? Looks like I would have to add a case for that as well in some of the programs I have made using the logical NOT. – Pranav Jituri Dec 19 '13 at 18:33
  • @PranavJituri - It's a little nebulous in theory, but for most practical purposes, expect a 1. If you really want to get technically correct, mask off the last bit and test for that. `n AND 1` will usually suffice. **Please be advised that I'm not up on current C standards**, hopefully someone had the courage to codify what `!0` returns. One way that is compiler quirk safe is defining TRUE as !0, and let the compiler implementation worry about what value that is, which will (hopefully) be internally consistent. – Brian Tiffin Dec 19 '13 at 18:42
  • @BrianTiffin - I get your point. For compatibility amongst different compilers and to have less logical errors I should compare as !0 = True value which (if depends) must rely on the compiler and I will have to worry less. Thanks for the tip. I am really happy to see people helping other people by giving various tips. – Pranav Jituri Dec 19 '13 at 18:45
  • 1
    @PranavJituri @BrianTiffin. I've already heard about this, so maybe there are compilers with results of unary `!` other than `0` or `1`. But K&R, C89, C99 and C11 are clear about that: The result is always `0` or `1`. ( for C99, C11: §6.5.3.3 (5) ) – mafso Dec 19 '13 at 19:24
  • @user1741125; thanks for the clarification. Fact is always better than 'think'. And I neglected to mention to Pranav that C also has the bitwise not operator ~, for inverting bits. – Brian Tiffin Dec 19 '13 at 19:31
  • @BrianTiffin - That is some extra knowledge today. I am currently on the chapter for Loop Control Instructions. Bitwise operators will be coming soon so will learn this operator at that time :) – Pranav Jituri Dec 19 '13 at 19:34

2 Answers2

7

The line

( !j != 1 ? printf("\nWelcome") : printf("GooD Bye")); 

is equivalent to

if(!j != 1)
    printf("\nWelcome);
else
    printf("Good Bye");  

Here !j evaluates to 0 therefore the condition !j != 1 will always be true and it will print Welcome.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Thanks for explaining it in this way, I was getting confused in the comparison part :ko: – Pranav Jituri Dec 19 '13 at 18:25
  • Lol, that is not a problem but yea sure I need to get 50 reps in order to add comments to questions of other people -_- – Pranav Jituri Dec 19 '13 at 19:16
  • Despiite of 50 reputation not a single reputation is added to my account (except the `15` for acceptance of the answer) [:'(](https://www.google.co.in/search?q=crying+smiley+pics&espv=210&es_sm=122&tbm=isch&source=iu&imgil=MOUQCTepiiicbM%253A%253Bhttps%253A%252F%252Fencrypted-tbn0.gstatic.com%252Fimages%253Fq%253Dtbn%253AANd9GcQTuzqLhSFcm8o0WgxYCsqY_4pzcDnzs5LCBJaPVJxNQX7qKZmC6w%253B500%253B500%253BQseEYWUpftXcWM%253Bhttp%25253A%25252F%25252Fgraphicleftovers.com%25252Fgraphic%25252Fcrying-smiley&sa=X&ei=-kazUpT2GIvxrQfMxYCAAw&ved=0CEcQ9QEwCw) – haccks Dec 19 '13 at 19:20
  • WHy do you need more reps :P – Pranav Jituri Dec 19 '13 at 19:21
  • Long story! I want to become people (programmer) like [Jon Skeet](http://stackoverflow.com/users/22656/jon-skeet), [Jonathan Leffeler](http://stackoverflow.com/users/15168/jonathan-leffler)...... many more, let it go. – haccks Dec 19 '13 at 19:24
1

yeah the confusion of c!!

Ok in c !j evaluates to 0, since it is a number that is not 0 so 0 != 1 is true there for the true part of the ternary operation is executed and "welcome" is printed.

so to revaluate:

!4 = 0 //or any number

!0 = 1

dbarnes
  • 1,803
  • 3
  • 17
  • 31