> For the complete documentation index, see [llms.txt](https://antoinearnoud.gitbook.io/fortran/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://antoinearnoud.gitbook.io/fortran/chapter8.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
