This question already has answers here:
Is 0 a decimal literal or an octal literal?
(3 answers)
Closed 9 years ago.
I'm using Visual Studio 2008 Express. If I debug this program, the variable num holds the value 322, even when it's initialized to 0502. What am I missing here?
int main()
{
int32_t num = 0502;
return 0;
}
int32_t is defined in the portable version of pstdint.h Version 0.1.12 as
typedef signed long int32_t;
0502 is octal, since it has prefix 0. 502 in octal is 322 in decimal.
If you start a integer value with 0 it is considered as an octal number, similarly 0x is used for hex.
Related
This question already has answers here:
How to work with Base 8 (Octal) numbers?
(3 answers)
Octal number literals: When? Why? Ever? [closed]
(13 answers)
Closed 3 months ago.
uint32_t number = 00000000000000000000000000001011;
std::cout << number;
Why is the number's value 521 here?
Number literals starting with a zero are interpreted as octal numbers (base 8).
1011 in base 8 is 521.
This question already has answers here:
Why is initializing an integer in C++ to 010 different from initializing it to 10?
(4 answers)
Closed 4 years ago.
What happened here?
long long value;
value = 001111011112;
in Visualstudio debugging, the value shows with value 153358922.
If the number doesn't start with 0, all is correct...
Any recommendations how to keep using my initialization style?
In C++, a number beginning by 0 is an octal (base 8) literal.
If you want to print this value with leading zeros, you can do:
long long value;
value = 1111011112;
cout << setw(12) << setfill('0') << value;
This question already has answers here:
Weird result after assigning 2^31 to a signed and unsigned 32-bit integer variable
(3 answers)
Closed 5 years ago.
Why does 1<<31 print 18446744071562067968 as output when we run the following code?
#include<iostream>
using namespace std;
int main(){
unsigned long long int i = 1<<31;
cout<<i; // this prints 18446744071562067968
}
1 is a signed int, which on your system is 32 bits. 1 << 31 results in overflow, and is a negative number (0x80000000). This, when converted to a 64 bit unsigned long long is then sign extended to 64 bits before being converted to the ULL value, which is 0xFFFFFFFF80000000, or the large decimal number you see.
This question already has answers here:
How does C Handle Integer Literals with Leading Zeros, and What About atoi?
(8 answers)
Closed 8 years ago.
Why does:
int test() {
return 00101 % 10;
}
return 5, while:
int test() {
return 101 % 10;
}
returns 1? I can't think of an explanation.
Integer literals beginning with 0 like
00101
is actually an octal constant.
00101 is octal value which is 65 in decimal so it returns 5.
00101 is in octal which is equal to 65 in decimal, so that is why the modulus operator will always give us 5.
You can do octal to decimal converstion on this link http://www.rapidtables.com/convert/number/octal-to-decimal.htm
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.
This code, when I run it on C++ Borland compiler 5.02
#include<iostream.h>
void main()
{
int x;
cin>>x;
cout << x;
}
When enter 0010 to x, the output is 8? And these for some entries that have zeroes on its left, but not all?
In C++:
Octal numbers have a leading 0.
Hexadecimal numbers have a leading 0x.
Otherwise, you have a decimal number.
Therefore:
0x10 is a hexadecimal number representing 16.
010 is an octal number representing 8.
10 is a decimal number representing 10.