Format Specifiers in C++

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Format Specifiers in C++.

You can change the format in which a value is displayed in the Watch window using format specifiers.

You can also use format specifiers in the Immediate window, the Command window, and even in source windows. If you pause on an expression in those windows, the result will appear in a DataTip. The DataTip display reflects the format specifier.

System_CAPS_ICON_note.jpg Note

The Visual Studio native debugger changed to a new debugging engine. As part of this change, some new format specifiers were added and some old ones were removed. The older debugger is still used when you do interop (mixed native and managed) debugging with C++/CLI. The following sections in this topic show the format specifiers for each debug engine.

If you have the following code:

int main() {  
    int my_var1 = 0x0065;  
    int my_var2 = 0x0066;  
    int my_var3 = 0x0067;  
}  

Add the my_var1 variable to the Watch window (while debugging, Debug / Windows / Watch / Watch 1) and set the display to hexadecimal (in the Watch window, right-click the variable and select Hexadecimal Display). Now the Watch window shows that it contains the value 0x0065. To see this value expressed as a character instead of an integer, in the Name column, after the variable name, add the character format specifier , c. The Value column now appears with 101 'e'.

WatchFormatCPlus1

The following tables show the format specifiers that you can use in Visual Studio. Specifiers in bold are not supported for interop debugging with C++/CLI.

SpecifierFormatOriginal Watch ValueValue Displayed
ddecimal integer0x00000066102
ounsigned octal integer0x00000066000000000146
x

 h
hexadecimal integer1020xcccccccc
X

 H
hexadecimal integer1020xCCCCCCCC
csingle character0x0065, c101 'e'
sconst char* string<location> “hello world”"hello world"
sbconst char* string<location> “hello world”hello world
s8const char* string<location> “hello world”"hello world"
s8bconst char* string<location> “hello world”"hello world"
suconst wchar_t* const

char16_t* string
<location> L”hello world”L"hello world"

u"hello world"
subconst wchar_t* const

char16_t* string
<location> L”hello world”hello world
bstrBSTR string<location> L”hello world”L”hello world”
s32UTF-32 string<location> U”hello world”U”hello world”
s32bUTF-32 string (no quotation marks)<location> U”hello world”hello world
enenumSaturday(6)Saturday
hvPointer type - indicates that the pointer value being inspected is the result of the heap allocation of an array, for example, new int[3].<location>{<first member>}<location>{<first member>, <second member>, …}
naSuppresses the memory address of a pointer to an object.<location>, {member=value…}{member=value…}
ndDisplays only the base class information, ignoring derived classes(Shape*) square includes base class and derived class informationDisplays only base class information
hrHRESULT or Win32 error code. (The debugger now decodes HRESULTs automatically, so this specifier is not required in those cases.S_OKS_OK
wcWindow class flag0x0010WC_DEFAULTCHAR
wmWindows message numbers16WM_CLOSE
!raw format, ignoring any data type views customizations<customized representation>4
System_CAPS_ICON_note.jpg Note

When the hv format specifier is present, the debugger attempts to determine the length of the buffer and display the appropriate number of elements. Because it is not always possible for the debugger to find the exact buffer size of an array, you should use a size specifier (pBuffer,[bufferSize]) whenever possible. The hv format specifier is intended for scenarios where the buffer size is not readily available

Size specifiers for pointers as arrays

If you have a pointer to an object you want to view as an array, you can use an integer or an expression to specify the number of array elements:

SpecifierFormatOriginal Watch ValuenValue Displayed
nDecimal or hexadecimal integerpBuffer,[32]

pBuffer,[0x20]
Displays pBuffer as a 32 element array.
[exp]A valid C++ expression that evaluates to an integer.pBuffer,[bufferSize]Displays pBuffer as an array of bufferSize elements.
expand(n)A valid C++ expression that evaluates to an integerpBuffer, expand(2)Displays the third element of pBuffer

Specifiers in bold are supported only for debugging native and C++/CLI code.

SpecifierFormatOriginal Watch ValueValue Displayed
d,isigned decimal integer0xF000F065-268373915
uunsigned decimal integer0x0065101
ounsigned octal integer0xF0650170145
x,XHexadecimal integer615410x0000f065
l,hlong or short prefix for: d, i, u, o, x, X004060420x0c22
fsigned floating point(3./2.), f1.500000
esigned scientific notation(3.0/2.0)1.500000e+000
ggsigned floating point or signed scientific notation, whichever is shorter(3.0/2.0)1.5
csingle character<location>101 'e'
sconst char*<location>"hello world"
suconst wchar_t*

const char16_t*
<location>L"hello world"
subconst wchar_t*

const char16_t*
<location>hello world
s8const char*<location>"hello world"
hrHRESULT or Win32 error code. (The debugger now decodes HRESULTs automatically, so this specifier is not required in those cases.S_OKS_OK
wcWindow class flag.0x00000040,WC_DEFAULTCHAR
wmWindows message numbers0x0010WM_CLOSE
!raw format, ignoring any data type views customizations<customized representation>4

Format specifiers memory locations in interop debugging with C++/CLI

The following table contains formatting symbols used for memory locations. You can use a memory location specifier with any value or expression that evaluates to a location.

SymbolFormatOriginal Watch ValueValue Displayed
ma64 ASCII characters0x0012ffac0x0012ffac .4...0...".0W&.......1W&.0.:W..1...."..1.JO&.1.2.."..1...0y....1
m16 bytes in hexadecimal, followed by 16 ASCII characters0x0012ffac0x0012ffac B3 34 CB 00 84 30 94 80 FF 22 8A 30 57 26 00 00 .4...0...".0W&..
mb16 bytes in hexadecimal, followed by 16 ASCII characters0x0012ffac0x0012ffac B3 34 CB 00 84 30 94 80 FF 22 8A 30 57 26 00 00 .4...0...".0W&..
mw8 words0x0012ffac0x0012ffac 34B3 00CB 3084 8094 22FF 308A 2657 0000
md4 doublewords0x0012ffac0x0012ffac 00CB34B3 80943084 308A22FF 00002657
mq2 quadwords0x0012ffac0x0012ffac 7ffdf00000000000 5f441a790012fdd4
mu2-byte characters (Unicode)0x0012ffac0x0012ffac 8478 77f4 ffff ffff 0000 0000 0000 0000

Size specifier for pointers as arrays in interop debugging with C++/CLIt

If you have a pointer to an object you want to view as an array, you can use an integer to specify the number of array elements:

SpecifierFormatExpressionValue Displayed
nDecimal integerpBuffer[32]Displays pBuffer as a 32 element array.
Show: