2
#include<stdio.h>
  int main() {
  int x;
  x=~!printf;
  printf("%x",x);
}

can some one explain me the process to derive the output of this program.

vinod kumar
  • 175
  • 2
  • 3
  • 6
  • 8
    Take it a piece at a time. What's the meaning of `printf` with no parentheses after it? What does the `!` operator do? What does the `~` operator do? – Sneftel Jul 25 '13 at 17:07
  • 2
    Strictly speaking, your program causes undefined behaviour, since you're trying to print an `int` with the `%x` format. – Carl Norum Jul 25 '13 at 17:11
  • 1
    @CarlNorum But isn't the `%x` format for printing an `int` in hexadecimal? – Some programmer dude Jul 25 '13 at 17:12
  • @CarlNorum that's not true. Specifying x without length defaults to unsigned int – SheetJS Jul 25 '13 at 17:12
  • 3
    @JoachimPileborg Strictly speaking, `%x` expects an `unsigned int`, not an `int`. Realistically, on most implementations, it will "work", except when the argument is negative, which it probably will be in this case - hence undefined (but probably not fatal) behavior.... – twalberg Jul 25 '13 at 17:14
  • @Nirk: So why is that 'undefined'? Won't it 100% of the time print whatever the value of `x` is as an unsigned integer? – Claudiu Jul 25 '13 at 17:14
  • 2
    “o,u,x,X The unsigned int argument is …” C99 7.19.6.1:8 “If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.” 7.19.6.1:9 – Pascal Cuoq Jul 25 '13 at 17:17
  • @PascalCuoq: ah gotcha, thanks. so by the spec it's undefined, but most compilers end up doing something more or less sane. – Claudiu Jul 25 '13 at 17:17
  • @Nirk, no it's still for `unsigned int`. I've never met a compiler that didn't "do the right thing", but it's still not strictly correct. – Carl Norum Jul 25 '13 at 17:18

1 Answers1

8
  • printf is a pointer to the printf function - thus it's ultimately an integer of some sort.
  • ! is unary NOT, meaning it returns 0 if the operand is true, and 1 is the operand is false. Since printf is true (non-zero, because the function is defined), the subexpression so far is 0.
  • ~ is bitwise complement. It flips all the bits of the binary number it is given. Since it is given 0, this will return 0xffffffff.
  • That result is then stored into x and printed out in hexadecimal.

On a 64-bit machine you might instead get 0xffffffffffffffff, though I'm not entirely certain.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • If someone could comment, I'm also not entirely sure what unary NOT returns. Various websites say it returns true or false, but AFAIK C doesn't have a true or false data type. So, if it returns 1 or 0, what is the bitwidth of that 1 or 0? – Claudiu Jul 25 '13 at 17:13
  • All of the logical operators (`!`, `!=`, `==`, `&&`, and `||`) return `int`. C99 (and newer) *do* have a boolean type, too. – Carl Norum Jul 25 '13 at 17:22
  • @CarlNorum: ah ok. and according to [wikipedia](https://en.wikipedia.org/wiki/C_data_types), `int` is defined as a signed integer which is at least 16 bits in size. so depending on what `int` is for the compiler & platform, you'll get the largest int possible for either 16-bits, 32-bits, 64-bits, etc.. – Claudiu Jul 25 '13 at 17:41
  • ... or 23 bits or 18 bits or 1384 bits or whatever, yes. – Carl Norum Jul 25 '13 at 18:02