152Chapter5 • Working with GNU Debugger
This GDB was configured as "i386-redhat-linux"...
(gdb) break main
Breakpoint 1 at 0x8048496: file sum.c, line 6.
(gdb) run
Starting program: /home/rr/5/sum
Breakpoint 1, main () at sum.c:6
6 printf("Enter first number : ");
(gdb) print num2
$1 = 134518424
(gdb) print total
$2 = 134513777
(gdb) n
7 scanf("%d", &num1);
(gdb) n
Enter first number : 45
8 printf("Enter second number : ");
(gdb) n
9 scanf("%d", &num2);
(gdb) n
Enter second number : 33
11 total = num1 + num2;
(gdb) print num2
$3 = 33
(gdb) n
13 printf("\nThe sum is : %d\n", total);
(gdb) n
The sum is : 78
14}
(gdb) n
Program exited with code 021.
(gdb)
•First set a
break
point where function
main
starts.
•Start the program execution using the
run
command.
•Print values of
num2
and
total
variables. Since these variables are not yet initialized,
random numbers are present at these two locations.
•Execute the program line by line and enter values of
num1
and
num2
variables and
then calculate the value of
total
.
•Print the value of
num2
variable which is now exactly what you have entered.
Note that in this session,
gdb
does not display the values of variables automatically when
a value changes. You have to use the
print
command again and again to display variable val-
ues. In the next section, you will see how to display these values automatically.
Next Page >>
<< Previous Page
Back to the Table of Contents