int x=011;
cout<<x;
it print 9. why?
Short, practical clarification (building on and ): in C and C++ a numeric literal that begins with a leading zero is an octal (base‑8) literal, not decimal. The language grammar defines octal literals as 0 followed by digits 0–7, so the source‑code token with a leading 0 is interpreted in base 8. (en.cppreference.com)
Important distinction often missed (answers ’s question): literal in source vs. user input. A source literal like 08 is invalid because 8 is not an octal digit — a compiler will diagnose it. Formatted input (e.g. std::cin >> n) is controlled by the stream’s base flags; streams are initialized with dec by default, so cin >> n reading the characters 08 normally yields decimal 8. If the stream is explicitly set to octal (std::oct) extraction uses octal rules (the library’s num_get selection maps oct → %o, hex → %x, dec → %d, and if a special “basefield==0” it would use %i-style auto-detection). Extraction sets failbit on error — check the stream state when in doubt. (en.cppreference.com)
Example patterns:
int y = 08; // invalid in source: compiler rejects '8' in an octal literal
// reading user input:
std::istringstream("08") >> n; // default: parsed as decimal -> n == 8
std::istringstream("08") >> std::oct >> n; // stream set to octal -> '8' is not valid; extraction fails (check .fail()) Practical tips: avoid accidental leading zeros in code; prefer explicit notation (0x for hex, 0b for binary in C++14+) or explicitly control stream base with std::dec/std::oct/std::hex. When parsing untrusted text, parse into a string first and convert with an explicit base (or use std::from_chars/std::stoi with a chosen base) and always check the stream or conversion result for errors. (en.cppreference.com)
Jump to Post— WaltP 2,905nitin1 is correct.
With a little more detail:
Any number that starts with a zero is interpreted in base 8 (octal) rather than base 10 (decimal). Therefore your 011 is:
one 8 and one 1rather thanone 10 and one 1.
— WaltP 2,905Jump to Post
Jump to Post— WaltP 2,905Because if you deleted the post, wouldn't all the replies look a little stupid? ;-)
it is a number in octal form as it has 0 in the starting. So it is 000 000 000 000 001 001, so this is nine as you well know ;) thanks
nitin1 is correct.
With a little more detail:
Any number that starts with a zero is interpreted in base 8 (octal) rather than base 10 (decimal). Therefore your 011 is: one 8 and one 1 rather than one 10 and one 1.
if i give input like 08 what will happen????
@all..one of the foolish question i have ever asked. as soon as i posted it i realized the answer.
@admin..why there is no option to delete a post?
Because if you deleted the post, wouldn't all the replies look a little stupid? ;-)
when there are no comments even then i cant delete it?
when there are no comments even then i cant delete it?
No. You can report it with the "Flag Bad Post" link and a reason stating why you want it deleted. Then a moderator will determine if the reason is...well, reasonable. But posts are typically only deleted based on serious rule violations such as illegal content, being complete spam, or copyright violation. We generally don't honor frivolous requests, but that doesn't mean you're not welcome to make them anyway on the off chance that it will be honored.
ok..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.