1
#include<stdio.h>
void main(){
  int i = 3;
  printf("%d", ~i);
}

The output is 2. 3 is 0000 0011. Tilde changes all the bit to their opposite. So how is the answer even 2? As I have read from other posts. 2's complement is (~i)+1 which makes ~ 1's complement operator. Even if it is so how is 2 a possible output?

Anagh Basak
  • 147
  • 9
  • 1
    `void main()` is wrong. `The output is 2` I cannot get this output. What compiler are you using? What compiler options are you using? What operating system are you using? – KamilCuk Oct 10 '20 at 14:39
  • The output here is `-4`. – Weather Vane Oct 10 '20 at 14:39
  • Where do you get such output? – anastaciu Oct 10 '20 at 14:39
  • What output? the result of `echo $?`, perhaps? – pmg Oct 10 '20 at 14:42
  • I am really sorry I checked again, the data type was int. Is using int changing things up? Well I am using gcc Mingw with windows 10. I just checked out with char and the answer is -4. So 1111 1100 is -4 in 2'complent. I don't understand 2's compliment well enough. and int being 4 bytes should make the answer 1111 1111 1111 1111 1111 1111 1111 1100. is it 2 in 2's complement ? – Anagh Basak Oct 10 '20 at 14:50
  • 1
    @AnaghBasak, it's the same, you could make an 8 bit representation 1111 1100 or even a 64 bit representation 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1100, the converted value is the same -4. – anastaciu Oct 10 '20 at 15:05
  • I don't know what was wrong with my system after trying for several rounds the answer is -4. – Anagh Basak Oct 10 '20 at 15:28

2 Answers2

4

I doubt the answer it's 2. It should be -4, which is the decimal representation of 11111100.

Online Run, which outputs:

-4

Indeed Two's complement is calculated by inverting the digits and adding one. So -4 + 1 = -3, as @WeatherVane commented.


PS: Unrelated to your question, but the main method typically returns an int, not void. Read more in What should main() return in C and C++?

Reference: Section 5.1.2.2.1 of the C11 standard (emphasis mine):

It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;10) or in some other implementation-defined manner.

10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and so on.

as @JérômeRichard commented.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0
  1. the main function should be int main(void)
  2. The answer is not 2 only -4 https://godbolt.org/z/b1GGv8
0___________
  • 60,014
  • 4
  • 34
  • 74
  • I have a question that is with normal functions the programmer gets to choose the return type. What's different with main(). Each program I ran main() with a void return type never produced an issue? What actually goes wrong when I change the return type to void? – Anagh Basak Oct 10 '20 at 14:53
  • Because standard says that. The question is like what will happen if I ignore the red light? Or stop sign? I did it many times before and nothing happened. So those lights and signs are not needed. I will **never** stop on the red lights. – 0___________ Oct 10 '20 at 15:01
  • Why does the standard say that what is the problem with void main? If I don't follow traffic signal I might end up in an accident. So according to your logic I might crash somewhere so where exactly? That is my question. IT's risky but what is the risk. Initilization is needed so that I don't end up dealing with garbage. wh – Anagh Basak Oct 10 '20 at 15:05
  • 1
    @AnaghBasak look at [Section 5.1.2.2.1 of the C11 standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf). The rational is that the host environment (ie. OS) need to know if the program finished correctly and without an `int`-compatible type as a return type (eg. `void`), the "status returned to the host environment is unspecified" (ie. undefined behavior on most OS). – Jérôme Richard Oct 10 '20 at 15:12
  • 1
    @AnaghBasak when you write a method in your program, e.g. `int read_file(char filename[16]);`, then you want, as the caller of this method to know if everything went well (because failures can happen, e.g. file could not be found). By checking the return value of the method you can know if everything went well or not, it's a mechanism for the method to signal you about its successful execution or not, and let the caller (you) decide how to handle an unexpected failure. So now imagine I write a C program for a micro-mechanism which is used in a more complex system. When the system calls the ... – gsamaras Oct 10 '20 at 15:13
  • 1
    ...micro-mechanism, it wants to know if everything went as expected, so that it can handle any potential error. If the C program's main method would return `void`, then how would the calling-system know about the execution of its subsystem (the micro-mechanism)? It cannot, that's why `main()` returns `int`, in order to communicate to its caller a successful (or not) execution. That was an example that helped me understand the signature when I was in university, hope this helps. :) – gsamaras Oct 10 '20 at 15:15
  • 1
    Thank you for such a gullible explanation. – Anagh Basak Oct 10 '20 at 15:19