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.
Related
This question already has an answer here:
How to know the number of non-zero elements of a Fortran array?
(1 answer)
Closed 1 year ago.
For example if I have a 1d array
indices=(/1, 1, 1, 2 , 2 ,10, 11 /)
I want to know how many times 1 occurs (the answer should be 3).
Number 2 should be 2 times, number 10 should be 1, number 11 should also be 1.
I already tried to find out if there is intrinsic function but the count function works differently.
You can use the count intrinsic with a comparison operator, e.g.
integer :: indices(7)
indices = [1, 1, 1, 2, 2, 10, 11]
write(*,*) count(indices==1)
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.
This question already has an answer here:
What's happening to an int that starts with 0? ex 00101 [duplicate]
(1 answer)
Closed 7 years ago.
Consider the following code:
int x = 030;
cout << x;
Prints 24 in Code::Blocks. Why?
It's an oktal number literal, that's the reason.
Cite from the reference:
octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)
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.
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