Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
using namespace std;
int main()
{
	int x;
	cout<<"Enter an int:";
	cin>>x;
	cout<<hex<<x<<endl
	    <<oct<<x<<endl;
	cout<<x<<endl;
	cout<<showbase<<dec<<x<<endl;
	cout<<showbase<<hex<<x;
	return 0;
}


My question is Why the last hex result is 0*43?why is not 43?
becuase result of this program is like this:
Enter an int:67
43
103
103
67
0x43

What I have tried:

I have searched about this and i saw this result:"When the showbase format flag is set, numerical integer values inserted into output streams are prefixed with the same prefixes used by C++ literal constants: 0x for hexadecimal values (see hex), 0 for octal values (see oct) and no prefix for decimal-base values (see dec)."
But does this have special cause that 0*or for octal it must be with 0?
Posted
Updated 13-Sep-16 14:26pm

1 solution

you've answered your own question

it's 0x43 because showbase inserts the 0x

You're not using showbase on the octal cout - if you were, line #2 would be 0103


(and it's an 'x', not an '*')
 
Share this answer
 
Comments
Member 12702056 13-Sep-16 22:16pm    
Thank you for your answer.I got that it's because of showbase,but does this have special cause that why showbase must insert this 0x for hexadecimal,0 for octal numbers?
barneyman 13-Sep-16 22:40pm    
because that's the standard way of expressing hex and octal, in programming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900