Why Is Initializing an Integer in C++ to 010 Different from Initializing It to 10

Why is initializing an integer in C++ to 010 different from initializing it to 10?

Because it's interpreting 010 as a number in octal format. And in a base-8 system, the number 10 is equal to the number 8 in base-10 (our standard counting system).

More generally, in the world of C++, prefixing an integer literal with 0 specifies an octal literal, so the compiler is behaving exactly as expected.

C++ int with preceding 0 changes entire value

An integer literal that starts from 0 defines an octal integer literal. Now in C++ there are four categories of integer literals

integer-literal:
decimal-literal integer-suffixopt
octal-literal integer-suffixopt
hexadecimal-literal integer-suffixopt
binary-literal integer-suffixopt

And octal-integer literal is defined the following way

octal-literal:
0 octal-literal
opt octal-digit

That is it starts from 0.

Thus this octal integer literal

0110

corresponds to the following decimal number

8^2 + 8^1 

that is equal to 72.

You can be sure that 72 in octal representation is equivalent to 110 by running the following simple program

#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::oct << 72 << std::endl;

return 0;
}

The output is

110

initialize long long with value starting with 0 leads to strange values

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;

Unable to understand result of this c program

011 is octal number due to 0 preceding it

011 = 1*(8^1) + 1*(8^0)
= 1*8 + 1*1
= 8 +1
= 9 in decimal(%d)

0x for hex and only digits(i.e 9) for decimal

Unsigned Number displays different results when a zero is appended to the number

Because the value represented by an octal literal is being printed out as decimal value on the standard output. If you want to output the octal literal value you should use the std::oct stream manipulator:

std::cout << std::oct << number;

Integer values are represented by integer literals. They can be octal such as 0437, decimal such as 287 or hexadecimal such as 0x11f and as of C++14 they can be binary literals such as 0b100011111. All of these literals represent the same value.

Why a[21] is not equal to a[021]?

In C++ a leading 0 on an integer literal means that value is in octal (similar to the way that 0x21 means that value is in hexadecimal).

Each of these values will be different. Here's a quick online demo:

http://cpp.sh/3cws4n

Note: the default output format for cout is decimal so the values you see are in decimal.

C++ cout output explanation please

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.

Initialisation and assignment

Oh my. Initialization and assignment. Well, that's confusion for sure!

To initialize is to make ready for use. And when we're talking about a variable, that means giving the variable a first, useful value. And one way to do that is by using an assignment.

So it's pretty subtle: assignment is one way to do initialization.

Assignment works well for initializing e.g. an int, but it doesn't work well for initializing e.g. a std::string. Why? Because the std::string object contains at least one pointer to dynamically allocated memory, and

  • if the object has not yet been initialized, that pointer needs to be set to point at a properly allocated buffer (block of memory to hold the string contents), but

  • if the object has already been initialized, then an assignment may have to deallocate the old buffer and allocate a new one.

So the std::string object's assignment operator evidently has to behave in two different ways, depending on whether the object has already been initialized or not!

Of course it doesn't behave in two different ways. Instead, for a std::string object the initialization is taken care of by a constructor. You can say that a constructor's job is to take the area of memory that will represent the object, and change the arbitrary bits there to something suitable for the object type, something that represents a valid object state.

That initialization from raw memory should ideally be done once for each object, before any other operations on the object.

And the C++ rules effectively guarantee that. At least as long as you don't use very low level facilities. One might call that the C++ construction guarantee.

So, this means that when you do

    std::string s( "one" );

then you're doing simple construction from raw memory, but when you do

    std::string s;
s = "two";

then you're first constructing s (with an object state representing an empty string), and then assigning to this already initialized s.

And that, finally, allows me to answer your question. From the point of view of language independent programming the first useful value is presumably the one that's assigned, and so in this view one thinks of the assignment as initialization. Yet, at the C++ technical level initialization has already been done, by a call of std::string's default constructor, so at this level one thinks of the declaration as initialization, and the assignment as just a later change of value.

So, especially the term "initialization" depends on the context!

Simply apply some common sense to sort out what Someone Else probably means.

Cheers & hth.,

I am not able to understand the output I am getting for printing an array of 1st eight array elements

Any integer literal, starting with a 0 is considered an octal representation. In your code, you're initializing some of your array members with octal numbers and then printing out the decimal representation (using %d format specifier) of them.

So, your expectation and the output differs.

Basically, your initialization, in decimal form, looks like

 int a[8] = {0,1,8,9,100,101,110,111}; //first 4 initializers are converted to decimal.

On the other hand, just for sake of completion, if you use the %o format specifier (octal representation), you can see the first four initializers, as you expected, but the last four also will be printed as octal, like

 printf("%o  ",a[i]);

will give you an output of

0 1 10 11 144 145 156 157



Related Topics



Leave a reply



Submit