If you find that you want to print the value of an expression frequently (to see how it changes), you might want to add it to the automatic display list so that GDB prints its value each time your program stops.
Each expression added to the list is given a number to identify it; to remove an expression from the list, you specify that number. The automatic display looks like this:
2: foo = 38 3: bar[5] = (struct hack *) 0x3804
This display shows item numbers, expressions and their current values. As with displays you request manually using x or print, you can specify the output format you prefer; in fact, display decides whether to use print or x depending on how elaborate your format specification is—it uses x if you specify a unit size, or one of the two formats (i and s) that are supported only by x; otherwise it uses print.
For example, display/i $pc can be helpful, to see the machine instruction about to be executed each time execution stops ($pc is a common name for the program counter; see "Registers").
The undisplay command doesn't repeat if you press Enter after using it. (Otherwise you'd just get the error No display number ....)
If a display expression refers to local variables, it doesn't make sense outside the lexical context for which it was set up. Such an expression is disabled when execution enters a context where one of its variables isn't defined.
For example, if you give the command display last_char while inside a function with an argument last_char, GDB displays this argument while your program continues to stop inside that function. When it stops where there's no variable last_char, the display is disabled automatically. The next time your program stops where last_char is meaningful, you can enable the display expression once again.