# Debugging

## Simple Thoughts

One problem with FORTRAN is that sometimes, when you try to use a variable to which you haven't given any initialization, it will still be able to use it. However, it will use the previous value stored at this place in the system. This value can be whatever crazy thing you can think about, since what was stored can be any type of data. That's how you can get crazy result.

First step of debugging is to make sure that all the variable you used have been initialized / given a value before using them.

## Debugging Tools

You can use gdb. When compiling, add the option `-g`. This allows you to run your program normally once it is compiled or to run it with gdb. Just type `gdb myprogram_exe`. Then you can type different things. For example

```
break main
run
```

and then follow the execution of the program step by step by using

```
step
```

To go faster one can use `step 50` which will automatically apply steep 50 times (50 can be changed to any integer).

Instead of typing `break main` (which puts break at each step of the program), you can instead create a break point anywhere in the code (source file) by using `break sourcefile.f90:lineofbreak`. After creating all the breaks needed, just use `run`.

Then, at any time you can use the

```
print name_of_variable
```

to see what is the value stored in a given variable.

`list`is used to see a few lines of code around the step that has just ran. Similarly, `where`tells you where you are in the program.

To quit gdb type

```
quit
y
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://antoinearnoud.gitbook.io/fortran/chapter8.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
