-8
int x = ~!printf;
printf("%d\t%x",x,x);

It gives : -1 ffff

Can anyone explain ?

Sitesh Roy
  • 387
  • 4
  • 8
  • 1
    convert function pointer to boolean, negate it logically-wise, then arithm-wise, to get all bits set, print. – Jean-François Fabre Dec 16 '17 at 09:31
  • 7
    Not contradicting Jean, I think the deeper meaning of that code is that the author attempted to intentionally confuse, possibly annoy the reader. Is the result used anywhere else? If yes I would be really interested for what. You could answer that by showing the using code. A more honorable intention would be to test you. You would not happen to do this for homework or for an exam your are taking? – Yunnosch Dec 16 '17 at 09:36
  • For booleans, 0 = false, everything else is true. printf is a function pointer and its address is surely not 0, so as boolean it would be true. This boolean value gets inverted to false (=0), then follows bitwise inversion (all 0 bits switch to 1) and saved as signed integer. A signed integer with all bits set is -1. printf prints this value for %d as decimal number, for %x in hexadecimal format. – Youka Dec 16 '17 at 09:38
  • @Youka Please make an answer to make the added value of your contribution more obvious, especially in comparison to the existing answer. – Yunnosch Dec 16 '17 at 09:40
  • 2
    So many robo-downvoters. such a shame as it's a decent question – Arun A S Dec 16 '17 at 10:00
  • 4
    @ArunAS decent question?! This is not decent question, it is not useful, and it is not well-researched. I see absolutely no attempts at either. Also, OP is using a 16-bit environment so this is most probably homework with Turbo C 2.0. – Antti Haapala -- Слава Україні Dec 16 '17 at 10:17
  • 3
    @ArunAS 'it's a decent question' what! It's rub.. rubbed me up the wrong way because it's deliberately designed to confuse by a prof/TA. It's of negative use to future visitors/users. Does that sound like a robo-review to you? Oh - and it's a 'do all my work for me' homework dump. – Martin James Dec 16 '17 at 10:23
  • oh sorry I forgot that SO is not very student friendly and is a place for professionals to ask professional questions so it can be answered by other professionals and not a place where those new to a language should bother asking their questions which seem rubbish to professionals ( sarcasm intended ) – Arun A S Dec 16 '17 at 10:40
  • 3
    @ArunAS You seem to assume "student-friendly" as "They're doing your homework". My definition would be: Support them in their own efforts when they get stuck rather than lift them over hurdles that they might even be paying for to learn something from. – tofro Dec 16 '17 at 10:44
  • I won't say to do their homework for them, I've seen many of those ( much worse than this ). But I'm just saying in general that SO has become too fierce ( there are many questions that deserve it, but there are many that are decent yet get a fierce reaction ). In this case indeed OP could have done some research, but saying this is rubbish question is what made me disappointed, OP provided a code, the result and asked what was happening ( the answer might not be quite obvious at first for those not used to programming ). If that's not considered good here, then I have nothing more to say – Arun A S Dec 16 '17 at 10:53
  • @ArunAS etc - where would performing logical operations on a function address be considered good? Why would anyone try to perform logical operations on a function address? Under what kind of possible use would such an operation be performed? – Martin James Dec 16 '17 at 12:25
  • The downvotes are for not-generally-useful, unclear questions, or questions that lack research... – Antti Haapala -- Слава Україні Dec 16 '17 at 14:40

1 Answers1

5

printf without arguments is the function pointer, worth a non-zero value (it's built-in so the pointer cannot be zero)

Now you apply logical negation (!) on this non-zero value: you get zero.

Now negate this zero bit-wise (using bit to bit negation ~), you get all 1s in the int bit range (which can vary depending on the compiler)

Printing it in decimal yields -1, and in hexadecimal yields a given number of fs, depending on the size of the integer (on my 32bit integer compiler, I get ffffffff)

(note that -specially the negation of the function pointer- cannot be part of some valid program, this is only for academic purposes)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 1
    negation of function pointer could be a test to see if the pointer is set at all. Of course `printf` is not a nullable function pointer here. – Antti Haapala -- Слава Україні Dec 16 '17 at 10:19
  • There is no guaranteed a null-pointer is all-zeros, so there is no guarantee a non-null-pointer value is not all-zeroes. `!` does not care about the actual value, but only whether it is a null-pointer for the implementation or not. Finally, the interpretation of the result of the bitwise-negation depends on the implementation's representation of signed integers. Also passing an `int` parameter where an `unsigned` (`%x`) is expected results in undefined behaviour. Negating a (function)pointer is well defined. Whether one writes `ptr == NULL` or `!p` is purely opinionated. – too honest for this site Mar 23 '18 at 22:42
  • yeah, lot of shortcuts here. the answer explains why it acts so given OP compiler (which is mainstream). – Jean-François Fabre Mar 23 '18 at 22:47
  • Concentrate on the UB part. A highly optimising compiler could just generate code to format the harddrive. Luckily that would require bad intend and complicate the compiler. Compiler writers are nice people and/or lazy, so they don't add such code. – too honest for this site Mar 23 '18 at 23:00