vinod kumar
vinod kumar

Reputation: 175

How to evaluate the following c program?

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

Upvotes: 2

Views: 201

Answers (1)

Claudiu
Claudiu

Reputation: 229561

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

Upvotes: 8

Related Questions