Why leading zeroes changes the value [duplicate] - c++

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)

Related

How to count number of occurences in a Fortran array [duplicate]

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)

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.

Meaning of X=X[:-forecast_out+1] [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
A snippet of regression code of a stock price data-
forecast_col='Adj. Close'
forecast_out=int(math.ceil(0.01*len(df)))
df['label']=df[forecast_col].shift(-forecast_out)
X=X[:-forecast_out+1]
What is the meaning of X=X[:-forecast_out+1] ?
This is a case of slice indexing, and also uses negative indices. It means that the last forecast_out + 1 elements of X are discared. For example,
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[:-2]
[1, 2, 3]

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