1

I don't know why the code below works? and why is it completed compile?

Code : b = char(a);

I wonder if it's one of the ways to use "char" or if it's a bug.

printf("Start Test\n");

int a = 0xF30;
char b = 0;
char c = 0;
char d = 0x30;
char e = 0xF30;
b = char(a);        // <- Why??
c = (char)a;    

printf("%c, %c, %c, %c, %c\n", a, b, c, d, e);

printf("End Test\n");

getchar();

return 0;

Console Result

Start Test

0, 0, 0, 0, 0

End Test
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 5
    Is it C or is it C++? – Keith Thompson Nov 01 '19 at 05:57
  • 2
    It's valid C++ but invalid C – Indiana Kernick Nov 01 '19 at 05:59
  • 1
    To expand on my earlier comment, it's almost never a good idea to tag a question as both C and C++. Specify the language that you actually used to compile the code. Some code is valid C and C++ (yours isn't, and that's the key to answering your question), but the language used is typically determined by which compiler you're using and by the name of the source file. – Keith Thompson Nov 01 '19 at 06:14
  • cpp file / vs2017 – tjdnfka2001 Nov 01 '19 at 07:28
  • With which compiler do you compile this code? Is it a .c- or a .cpp-file? – RobertS supports Monica Cellio Nov 01 '19 at 07:50
  • @AnttiHaapala: You downvoted the question for a problem that it no longer has because you fixed it yourself. A downvote usually indicates a problem that the OP could correct. In this case, there's nothing the OP could do. Please consider withdrawing your downvote. – Keith Thompson Nov 01 '19 at 07:58
  • @RobertS: Answered in the comment before yours. – Keith Thompson Nov 01 '19 at 08:03
  • Please consider adding meaningful titles - "char() issue please help me" is not at all indicative of the problem you have. I've removed the pleading, but it could still be improved. "Why does a variable of type X allow an assignment to a value of type Y" could be a good format (with, obviously, the actual types inserted into that sentence). I don't understand the problem though, so I will leave someone else to make that edit. – halfer Nov 02 '19 at 10:45

3 Answers3

3

This is covered under the section "Explicit type conversion (functional notation)" in the C++11 standard.

Given your code,

b = char(a);

is the same as

b = a;

More generally, it can be used to explicitly convert an expression to another type. E.g.

b = char(10);

Here, 10 is of type int. By using char(10), 10 is explicitly converted to char before the assignment.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
3

C++ (unlike C) has more than one notation for explicit type conversions (casts).

C++ inherits C's cast notation:

int i = some_value;
char c;
c = (char)i;

It also has a functional notation:

c = char(i);

as well as several cast operators which are more restrictive than a C-style cast.

c = static_cast<char>(i);

These all (in this case) specify the same conversion.

Note that for conversions between numeric types, a cast is not necessary (and is probably a bad idea). An assignment (or a function argument, or a return statement) will perform an implicit conversion, so you can just write:

c = i;

By using a cast, you risk specifying the wrong type, resulting in an additional implicit conversion and possibly a wrong result:

c = (unsigned char)i; // explicitly converts to unsigned char,
                      // then implicitly converts to char
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

As mentioned in comments, b = char(a); It is Invalid operation in c, but valid in c++.In c++ it will be considered as a ascii value. so it is same as this notation b = (char)a;

VJAYSLN
  • 473
  • 4
  • 12