C++ cout output explanation please [duplicate] - c++

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.

Related

Typecasting char to int C++ [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed last year.
I've found that when I typecast a character pointer to in C++17, I get a some sort of mapping instead of the actual number I would expect. Example below
#include <iostream>
int main () {
char c;
c = '1';
std::cout << int(c) << std::endl;
}
When I build and run this with
g++ file.cpp -o output
./output
I get
49
'0' maps to 48 and '2' maps to 50 and so on. Why? How do I avoid this?
What you are actually getting is the ASCII code for these characters because they are stored in memory as integers known as ASCII codes.
To convert a char variable to its decimal value instead you can use this:
int value = c - '0';
What this does is that it takes the integer value of c which is 48 for the '0' for example and subtracts the integer value of '0' from it which is also 48, resulting in 0.
Below is the full table for decimal digits and their corresponding ASCII values:
0 -> 48
1 -> 49
2 -> 50
3 -> 51
4 -> 52
5 -> 53
6 -> 54
7 -> 55
8 -> 56
9 -> 57
And when subtracting the '0' from them the result is their corresponding decimal values:
0 -> 48 - 48 = 0
1 -> 49 - 48 = 1
2 -> 50 - 48 = 2
3 -> 51 - 48 = 3
4 -> 52 - 48 = 4
5 -> 53 - 48 = 5
6 -> 54 - 48 = 6
7 -> 55 - 48 = 7
8 -> 56 - 48 = 8
9 -> 57 - 48 = 9
I've found that when I typecast a character pointer to in C++17, I get
a some sort of mapping instead of the actual number
In the provided code there are no pointers. There is an object of the type char.
char c;
c = '1';
This statement
std::cout << int(c) << std::endl;
outputs the internal representation of the character '1' as an integer. In ASCII the internal representation of the character '1' is decimal 49. In EBCDIC the internal representation of the character '1' is 241.
If you want to get the integer number 1 you could write for example
std::cout << c - '0' << std::endl;
Or you could output the character as character and the output on the console will be the same that is 1
std::cout << c << std::endl;

Unexpected behaviour/bug in function to find radical of an integer [duplicate]

This question already has answers here:
std::pow with integer parameters, comparing to an integer type
(4 answers)
Closed 5 years ago.
I have written a short C++ function which is trying to return similar to the the radical of an integer, but instead the smallest integer which is some root of the the given number. The general logic is that the function decomposes the integer into prime powers, looks for the gcd of all the exponents of the prime powers, then finds the radical by dividing all the exponents by this gcd. I thought the function was working as intended, however I noticed that when passing the function 13 as an argument it returns 12, I have put the output for 2 to 20 below; everything is correct apart from 13. When trying to debug this both "primes" and "powers" hold the correct values (13 and 1 respectively, both vectors of size 1) as well as "power" being 1 as it should. So the problem must be in the last loop, but I am really very confused as to what could be happening as it should be calculating pow(13, 1/1).
#include<vector>
#include<cmath>
int radical(int n){
int power = 0;
std::vector<int> primes;
std::vector<int> powers;
for(int i = 2; n != 1 ; ++i){
int t_pow = 0;
while(n % i == 0){
++t_pow;
n /= i;
}
if(t_pow != 0){
primes.push_back(i);
powers.push_back(t_pow);
}
power = (power == 0 ? t_pow : gcd(power, t_pow));
}
int rad = 1;
for(unsigned i = 0u; i < primes.size(); ++i){
rad *= pow(primes.at(i), powers.at(i)/power);
}
return rad;
}
2 2
3 3
4 2
5 5
6 6
7 7
8 2
9 3
10 10
11 11
12 12
13 12
14 14
15 15
16 2
17 17
18 18
19 19
20 20
EDIT: The most efficient way to implement an integer based power function pow(int, int)
The naive algorithm looks pretty simple actually:
rad = 1
i = 1
While n > 1:
increase i
if i divides n:
multiply rad by i
while i divides n, divide n by i
You're already performing all these steps, the fact is you're doing much more than that (and using much more space), so why not just get rid of unnecessary actions?
define your own int powi(int,int) function and check if the error petsists

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

Print Integer in C/C++ [duplicate]

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.

left shifting in between two different unsinged integer values [duplicate]

This question already has answers here:
Confusing operator precedence: a << b + c << d
(4 answers)
Closed 8 years ago.
when am I applying left shifting to 1 by 4 bit as 1<<4 it printing 16 as the value but if I will apply shifting like 1<<4 + 1<<3 then it printing 256 as a result,
I am not getting, how many shifting is apply and how it is working?
but according to me answer should be 24 by applying 4 left shift + 3 shift
1 << 4 + 1 << 3
Is actually interpreted as
(1 << ( 4 + 1 )) << 3
See?
1 << 5 --> 32
32 << 3 --> 256
When in doubt, use more parentheses!
(1 << 5) + (1 << 3) --> 24