Quiz on programming fundamentals 1, doubt

In this program in line 4th we have used signed without using int or so… we have been taught that it is written like signed int a=12 etc… so it should be wrong, also in line 6 it is written as signed char, but how can we use signed or unsigned with character, in line 5th also we have used signed or unsigned without using int or float etc?
how are these things possible?

  1. #include
  2. using namespace std;
  3. int main(){
  4. signed CodingBlocks = 9;
  5. signed Nagarro = A;
  6. signed char HackerBlocks = ‘A’;
  7. cout<<CodingBlocks<<endl;
  8. cout<<HackerBlocks<<endl;
  9. return 0;
    }

@Sheenagoyal21
In case no other data type is specified such as in the statement
signed CodingBlocks = 9 ;
The compiler assumes it to be int by default.
This also works for long.
long int x = 9 ;
long x = 9 ;
Both these statements are equiavalant. Compiler takes int by default if no other data type is specified with the identifier.

As for signed char HackerBlocks = ‘A’ , as you know characters are stored as ASCII values in C++ ranging from 0 to 255.
If you were to declare a normal character and pass them to int variable , they would print the ASCII value.
char ch = ‘A’;
int y = ch ;
cout << y ; //Prints 65 - ASCII value of ‘A’

We can change it store the values not as 0 to 255 but as -127 to +127.
signed char ch = ‘A’
So if you were to simply print the character it would still print the character only.
cout << ch ; //Prints A

However , if you were to convert it to int now .
int x = ch ;
cout << x ;

The result of this is not defined and totally depends on the C++ standards defined by the compiler you are using.
It may print 65 only or it could print -62 ( -127 + 65 ).
While the conversion is guaranteed , its implementation could vary across compilers .
( It is guaranteed it would be either or the two , there won’t be a third answer. )

For the above problem , since we directly printing the variable HackerBlocks , we do not have to worry about this issue and the answer for this statement
cout<<HackerBlocks<<endl;
would simply be ‘A’.