Skip to content
Advertisement

How to print a binary value(1010) into decimal value(10) in GDB?

I want to print the decimal value of 1010 in gdb, but it prints the result as it is what I gave last.

(gdb) 
(gdb) p/d 1010
$1 = 1010
(gdb)

Advertisement

Answer

GDB’s p[rint] command prints the value of the expression you provide, which is interpreted in the source language of the program being debugged. In C, your 1010 is a decimal literal, not a binary literal, so your basic problem is that you are giving GDB bad input.

Standard C has no support for binary literals, but GNU C supports them as an extension. The format is a binary digit string preceded by 0b or 0B, which you’ll probably recognize as analogous to the standard format for hexadecimal literals. GDB recognizes this form.

Since print‘s default output radix for numbers is decimal, you don’t need to specify an output format. Just use the command

p 0b1010
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement