I am working on below code:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
It is printing 1 and some garbage.
Why cout
is behaving so?
I am working on below code:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
It is printing 1 and some garbage.
Why cout
is behaving so?
cout << a
is printing a value which appears to be garbage to you. It is not garbage actually. It is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 1
is non-printable. You can check whether a
is printable or not using, std::isprint
as:
std::cout << std::isprint(a) << std::endl;
It will print 0
(read: false
) indicating the character is non-printable
--
Anyway, if you want your cout
to print 1
also, then cast a
to this:
cout << static_cast<unsigned>(a) << std::endl;
I had a similar issue here that I've long forgotten about. The resolution to this problem with iostream's
cout
can be done like this:
#include<iostream>
#include<stdio.h>
main() {
unsigned char a;
a=1;
printf("%d", a);
std::cout<< +a << std::endl;
return 0;
}
instead of casting it back to another type if you want cout
to print the unsigned char
value as opposed to the ascii
character. You need to promote
it.
If you noticed all I did was add a +
before the unsigned char
. This is unary addition that will promote the unsigned char
to give you the actual number representation.
User Baum mit Augen is responsible for reminding me of this solution.
You need to typecast a
as integer as cout<< (int)(a);
. With this you will observe 1
on the output. With cout << a;
, the print will be SOH (Start of Heading)
corresponding to ascii value of 1
which can't be printed and hence, some special character is observed.
EDIT:
To be more accurate, the cout
statement should be cout << static_cast<unsigned>(a)
as Nawaz has mentioned.
The C compiler has its own way of defining the type of the printed output, because you can specify the type of the output.
Ex:
uint8_t c = 100;
printf("%d",c);
so you can also print c
as an int by %d
, or char %c
, string %s
or a hex value %x
.
Where C++ has its own way too, the cout
prints the 8-bit values as a char
by default. So, you have to use specifiers with the output argument.
You can either use:
a +
before the name of the output argument
uint8_t data_byte = 100;
cout << "val: " << +data_byte << endl;
use a function cast unsigned(var)
; like,
uint8_t data_byte = 100;
cout << "val: " << unsigned(data_byte) << endl;
A char
will have a numerical value associated with it. The character represented by 1
is the SOH
or Start of header
character which is usually not printed (similar to printing the NUL
character \0
).
You can have a look at an ASCII table to see which character will be printed, but an example with a value such as 65 might help. Where 65 is the A
characater.
#include<iostream>
#include<stdio.h>
int main() {
// initialise with an integer
char a = 65;
// print the integer representation
printf("%d\n", a);
// print as a character
std::cout << a << std::endl;
// initialise with a character
char b = 'B';
// print the integer representation
printf("%d\n", b);
std::cout << static_cast<int>(b) << std::endl;
// print as a character
std::cout << b << std::endl;
}
Outputs:
65
A
66
66
B
printf("%u",a); its so simple try it