0

I'm reading the source code of AVChat. It's a video chat program using UDP and DirectShow. In the header file GlobalDef.h, however, I find some definitions as below:

// Messages
const long msg_FilterGraphError = 'avct' + 1;
const long msg_MediaTypeReceived    = 'avct' + 2;
const long msg_TCPSocketAccepted    = 'avct' + 3;
const long msg_UDPCommandReceived   = 'avct' + 4;
const long msg_ModifyFilterGraph    = 'avct' + 5;

// Let the main thread modify filter graph
#define WM_ModifyFilterGraph        (WM_USER+123)

// UDP command defines
const long MAX_COMMAND_SIZE     = 100;
const long cmd_ClientCalling    = 'avct' + 100;
const long cmd_DeviceConfig     = 'avct' + 101;
const long cmd_BuildFilterGraph = 'avct' + 102;
const long cmd_DisconnectRequest    = 'avct' + 103;

I thought '' is used to surround a single char, so why this code runs without problem on my VS2010? These long consts are used as commands sent from client to server. I've set a breakpoint to watch the value, and VS tells me 'avct' = 1635148660. I've also tried to search for 'avct' in the entire solution and find no match except these. So please someone tell me how is the value of 'avct' is generated.

EDIT: I find that if you put multiple characters between '' and feed it to a char variable, only the last character is transferred. That can explain why 'avct' won't report an error, but I still don't know how the value is generated.

Community
  • 1
  • 1
Manas
  • 598
  • 3
  • 14
  • 1
    Multicharacter literals are `int`s and have an implementation-defined value. You can't count on the last one being the only one that matters with `char`s. – chris Jul 11 '13 at 07:42

2 Answers2

1

Historically, the original C accepted multi-character character constants, and both C and C++ still do, on historical grounds. Unlike single character constants, the type is int, and the value is implementation defined (but will typically consist of some sort of combination of the characters involved).

Practically speaking, they should be avoided in new code, and cannot be used in portable code (because implementations do vary as to what they mean).

EDIT:

For what it's worth: the most typical implementation would be more or less the equivalent of:

union
{
    char c[sizeof(int)];
    int i;
};

, placing the characters in order in c (and ignoring any which didn't fit—whether the first or the last depending on the implementation), and then use the value of i as the value. These results obviously depend on the encoding (but that's true of any character constant), but also on things like byte order and the size of an int. Thus, even assuming an ASCII based encoding, on systems I've used, the results could be 0x61766374, 0x74637661, 0x6374, 0x7463, 0x6176 or 0x7661. (And this doesn't consider "exotic" architectures with 9 bit bytes, or where the size of an int is 6.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Thanks to @chris I know what to search on stackoverflow (I'm not an English speaker so find the right words is a little tricky): What do single quotes do in C++ when used on multiple characters?

I think it makes my question a duplicate so I'll answer it myself.

'a'=0x61
'v'=0x76;
'c'=0x63
't'=0x74
'avct'=0x61766374=1635148660
Community
  • 1
  • 1
Manas
  • 598
  • 3
  • 14
  • Except that this answer is wrong. The first four lines of your example suppose an ASCII based encoding (usually the case); the final line supposes some particular implementation, and implementations _do_ vary here. – James Kanze Jul 11 '13 at 08:08
  • @JamesKanze Yes so I'm not using it myself. Just help to understand the code. – Manas Jul 11 '13 at 08:11
  • The first thing you can understand is that it was either written a very long time ago, in the earliest days of C, or by a very poor programmer. The actual binary values could differ from one compiler to the next. Or even from one version of the compiler to the next. – James Kanze Jul 11 '13 at 08:16