0

I am unable to understand the output produced by the following code:

#include <stdio.h>

int main()
{
    int var = 010;
    printf("%d", var);
}

The output of the above code is 8.

amirali
  • 1,888
  • 1
  • 11
  • 32
NITISH KUMAR
  • 59
  • 2
  • 7
  • Please, read https://meta.stackoverflow.com/a/253896/2988 to understand why "Explain this code to me" questions are off-topic, and what you can do to make it on-topic. – Jörg W Mittag Jun 23 '19 at 14:02
  • @JörgWMittag: That question is really about substantial code sequences where the issue lies in algorithms embedded in the code. It is not about question on simple interpretations of C elements. Furthermore, it is absurd to say it is too broad when it is concisely answered as specified by C 2018 6.4.4.1. – Eric Postpischil Jun 23 '19 at 14:05
  • @Eric. But this one happens to be a duplicate regardless – Mad Physicist Jun 23 '19 at 14:07
  • @MadPhysicist: That is nice, but it is irrelevant to whether the question is too broad. We should not be discouraging participation in Stack Overflow by falsely telling people a proper (albeit duplicate) question violates some rule or guideline that it does not. – Eric Postpischil Jun 23 '19 at 14:09
  • @EricPostpischil: There is zero indication in the question about what exactly the OP is confused about and which parts of the code he understands and doesn't understand. The code snippet uses the preprocessor, functions, types, statements, expressions, local variables, declarations, the standard library, integers, and probably many other things I am forgetting. Without a clear statement about what, precisely, the OP is confused about, you have to explain them all. – Jörg W Mittag Jun 23 '19 at 14:11
  • @Eric agreed. A precise reason for closing is important. OP should know what to fix in the future. – Mad Physicist Jun 23 '19 at 14:11
  • @Jörg. Between the title and the question itself, there's really very little ambiguity. Now you're starting to imply that C code shouldn't contain the necessary boiler plate, because it introduces ambiguity... – Mad Physicist Jun 23 '19 at 14:13
  • @EricPostpischil: note that within 7 minutes of asking the question we already have one answer explaining octal literals, and another answer explaining the `%d` format specifier for `printf`. – Jörg W Mittag Jun 23 '19 at 14:13
  • @MadPhysicist: The title "How to interpret the output of the following code" could just as well be interpreted as asking about `printf`, which indeed one answer took it to mean. – Jörg W Mittag Jun 23 '19 at 14:15
  • The title `What is the number `010` in C?` is pretty clear though, you must admit. – Mad Physicist Jun 23 '19 at 14:17
  • @MadPhysicist: That title was edited by someone other than the OP who made an assumption about what the OP might have meant *after I wrote my comment*. The OP's title at the moment I wrote my comment was "How to interpret the output of the following code". Since I do not own a time machine, I could not possibly have known that the title would be edited later. Interestingly, the person who edited the title to be only about the literal also wrote an answer explaining `printf`, so apparently, they are not even 100% sure their interpretation of the question is correct. – Jörg W Mittag Jun 23 '19 at 14:18
  • @JörgWMittag. Thanks for explaining. I wasn't aware of the changes. Your comments make a lot more sense now. I have to agree that it's unclear what the best reason to close the question is at this point. But at least we all agree that there is good reason to close it. – Mad Physicist Jun 23 '19 at 14:39

2 Answers2

1

A leading 0 introduces an octal constant, so 010 is octal which is 8 in decimal. If you want binary, write 0b010 (which is 2 in decimal).

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
gavinb
  • 19,278
  • 3
  • 45
  • 60
1

010 is an integer constant (i.e. literal), encoded in octal:

001 == 1
002 == 2
...
007 == 7
010 == 8

When you call printf with a format specifier %d, it prints the value of the given signed integer encoded in decimal, and therefore, you will see the character 8 written to the output.

Acorn
  • 24,970
  • 5
  • 40
  • 69