Print Integer in C/C++ [duplicate] - c++

This question already has answers here:
printf with "%d" of numbers starting with 0 (ex "0102") giving unexpected answer (ex '"66")
(4 answers)
Closed 7 years ago.
I have a simple program.
#include <cstdio>
int main()
{
int num = 000012345; printf("%d\n",num);
return 0;
}
The above program gives 5349. Why ? I mean it should be wrong, but why 5349 ?

Numbers starting with 0 are octal in c/c++.
Octal = 000012345
Decimal= 0×8⁸+0×8⁷+0×8⁶+0×8⁵+1×8⁴+2×8³+3×8²+4×8¹+5×8⁰ = 5349
Binary = 1010011100101
Hex = 14E5

A number starting with one or more leading zeros specifies octal format instead of decimal. So 000012345 is 1 * 8^4 + 2 * 8^3 + 3 * 8^2 + 4 * 8^1 + 5 * 8^0 = 5349.
Similarly, a number starting with 0x is in hexadecimal format.

Related

C++: Why are these results different? [duplicate]

This question already has an answer here:
C printf %d incorrect value with leading zeros? [duplicate]
(1 answer)
Closed 4 years ago.
Why is the output of these two examples different in C++?
int a=025;
float b=5.5;
cout<<a+b;
26.5
int a=25;
float b=5.5;
cout<<a+b;
30.5
From cppreference:
octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)
So 025 is actually the octal literal corresponding to decimal 21 which is why your answers differ by 4 (25-025 or 25-21).
In the first example, you have assigned "a" the value of an octal literal. It's not the same as decimal 25. It's equivalent to 21 decimal.

Bitwise OR Operator [duplicate]

This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
What does it mean when a numeric constant in C/C++ is prefixed with a 0?
(7 answers)
Octal number literals: When? Why? Ever? [closed]
(13 answers)
Closed 5 years ago.
I have the following piece of code:
int ret() {
int x = 010;
int y = 4;
int z = x | y;
return z;
}
When x = 010, this function returns 12. However, on changing x to 10, 14 is returned. Why is this so?
The OR operator is a red-herring: the issue is elsewhere.
010 is an octal literal due to the leading 0. In decimal, this is 8.
So x has the value 8 in decimal. And 8 | 4 is 12.
10 is a decimal literal. And 10 | 4 is 14.

Why int x = 01234 gives outpiut value of x as 668? [duplicate]

This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 5 years ago.
I recently came across the following when I was testing my code for various value of x.
I will try to illustrate only the issue.
#include <iostream>
int main()
{
int x = 01234;
std:: cout << x ;
return 0;
}
Output:
when x = 1234 , 1234
x = 01234 , 668
x = 001234 , 668
x = 240 , 240
x = 0240 , 160
x = 00240 , 160
For mostly any number starting with 0 I get a different value.
eg: x = 0562 gives 370 and so on.
I tried using various online C++ compilers but all give same output.
I tried to google the issue but couldn't find an appropriate answer.
Looks like you've been hit with an octal literal! Any number literal beginning with just 0 is interpreted in base 8.
01234 = 1 × 8^3 + 2 × 8^2 + 3 × 8^1 + 4 × 8^0
= 1 × 512 + 2 × 64 + 3 × 8 + 4 × 1
= 512 + 128 + 24 + 4
= 668
0240 = 2 × 8^2 + 4 × 8^1 + 0 × 8^0
= 2 × 64 + 4 × 8 + 0 × 1
= 128 + 32
= 160
The number 01234 is in octal (base 8) when you prepend a 0 you define the number as an octal. When you then print it in decimal you get it's decimal equivalent

Why are integers converted to octal numbers here? [duplicate]

This question already has answers here:
What does it mean when a numeric constant in C/C++ is prefixed with a 0?
(7 answers)
Closed 7 years ago.
I am unable to understand output of below mentioned program-
#include <stdio.h>
int main()
{
int i, a[8]={000, 001, 010, 011, 100, 101, 110, 111};
for(i=0;i<8;i++)
{
printf("%d\t",a[i]);
}
system("pause");
return 0;
}
OUTPUT -
0 1 8 9 100 101 110 111
Why are the initial four values getting converted here???
Any integer literal that starts with a 0 followed by other digits is octal, just like any integer literal starting with 0x or 0X, followed by digits, is hexadecimal. C++14 will add 0b or 0B as a prefix for binary integer literals.
See more on integer literals in C++ here.
If you start a number with a 0 it gets converted to an octal number
0xNumber is hex

C++ cout output explanation please [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why is initializing an integer in VC++ to 010 different from initialising it to 10?
This got me very confused, and I hope one of you can answer my question. How come this code will produce the output "116"?
#include <iostream>
int main()
{
std::cout << 0164 << std::endl;
return 0;
}
The code has been compiled with MSVC++ 2010 and g++ under Mac OS X.
"cout" can print '0' alone and '164' alone, but as soon '0' is the first digit in the number the output changes.
Because the 0 in front makes the number be interpreted as octal.
0164 =
4 * 1 +
6 * 8 +
1 * 64
= 116
Or, via binary:
0164 =
0 1 6 4 =
000 001 110 100 =
1110100 =
116
The same goes for hexadecimal numbers, you write them as 0x1FA for example.
In C and its brethren, a number with 0 on the front is octal, not decimal.
Hence your number is 1 * 82 (1 * 64 = 64) plus 6 * 81 (6 * 8 = 48) plus 4 * 80 (4 * 1 = 4) which equates to 116.
See here for a large treatise on what hexadecimal and octal are in C.