{"id":396027,"date":"2025-12-07T15:56:37","date_gmt":"2025-12-07T14:56:37","guid":{"rendered":"https:\/\/dutable.com\/?p=396027"},"modified":"2025-12-07T16:44:50","modified_gmt":"2025-12-07T15:44:50","slug":"debugging-c-programs-with-gdb","status":"publish","type":"post","link":"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/","title":{"rendered":"Debugging C Programs in Unix with GDB: A Beginner\u2019s Guide"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#What_is_GDB\" >What is GDB?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Installing_GDB_An_Easy_Case\" >Installing GDB: An Easy Case<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Running_the_Program_in_GDB\" >Running the Program in GDB:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Basic_GDB_Commands\" >Basic GDB Commands<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Setting_Breakpoints\" >Setting Breakpoints<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Tracing_Logic_Errors\" >Tracing Logic Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#The_Debugging_of_Advanced_C_Programs\" >The Debugging of Advanced C Programs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/dutable.com\/debugging-c-programs-with-gdb\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">The skill of debugging is a very important skill that a programmer must develop. Regardless of whether you are working alone on a project, or you are part of a team, you need to be able to find and resolve any problem in your code quickly, and this is success. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article shall present <strong>GNU Debugger (GDB)<\/strong> which is an ardent tool used in C programming to enable a programmer to step-through his\/her program, examine the variables, set breakpoints and to debug logic errors. In case you are working on a Unix-based system, understanding GDB will go a long way in making you efficient and understanding system in your C programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_GDB\"><\/span><strong>What is GDB?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>GNU Debugger (GDB)<\/strong> is an application that enables developers to debug programs that are written in the C (and other such languages as C++, Fortran, and Assembly) language. The concept of ensuring that the mistakes or bugs in your code are detected and fixed to ensure that your program works is referred to as debugging. GDB will have a range of commands that enable the programmer to stop the execution of the program, examine the values of the variables and step through the code line by line. This is what makes it a precious tool to any person writing C programs on a Unix environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, it is necessary to have GDB installed on your system. Majority of Unix-based systems have GDB installed. Otherwise, it is easy to install it via your package manager.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installing_GDB_An_Easy_Case\"><\/span><strong>Installing GDB: An Easy Case<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s first go through the setup of GDB and execution of a simple program before getting into the debugging methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Write a Simple C Program:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We are going to write a simple C program with a mistake in it. The sample of a program to add two numbers is this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">#include &lt;stdio.h&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int main() {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;int a = 5;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;int b = 10;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;int sum = a + b;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;a + b = a + %d = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;return 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Assemble the Program with Debugging Script:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In order to use GDB you must compile your C program with debug information. This may be accomplished by passing the -g flag to GCC:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -g program.c -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The -g flag has the advantage that GDB is able to debug the program source code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Start GDB:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105.png\" alt=\"Screenshot of the GDB terminal showing a C program execution with commands like &#039;run&#039;, &#039;next&#039;, and &#039;print&#039; used for debugging.\" class=\"wp-image-396032\" title=\"\" srcset=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105.png 1024w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105-300x300.png 300w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105-150x150.png 150w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105-768x768.png 768w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-105-500x500.png 500w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">GDB can now be started with the debugging information built into the program, by typing:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gdb .\/program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_the_Program_in_GDB\"><\/span><strong>Running the Program in GDB:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you are in the GDB prompt you can type run to start the program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(gdb) run<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_GDB_Commands\"><\/span><strong>Basic GDB Commands<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Implementing Your Program, Step-by-Step<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After GDB is executing your program, you can use a number of commands to control the execution of your program and aid yourself in debugging it more easily.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>run<\/strong>: Start your program.<br><\/li>\n\n\n\n<li><strong>next<\/strong>: Move to the following line of code.<br><\/li>\n\n\n\n<li><strong>step<\/strong>: Step in a function to check its behavior.<br><\/li>\n\n\n\n<li><strong>continue<\/strong>: Resume the program to the next breakpoint or termination point.<br><\/li>\n\n\n\n<li><strong>quit<\/strong>: Exit GDB.<br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to go through the program line-by-line, you can use the next or step command. This will allow you to see the way the variables vary and identify the problems in time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Inspecting Variables<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ability of GDB to examine variables at any stage of program execution is one of the most powerful capabilities of GDB.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print<\/strong>: This command is used to print the value of a given variable.<br><br>(gdb) print sum<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&nbsp;This will be a representation of the value of the sum variable at the present state of the program.<br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>display<\/strong>: This is like the print command; however, it uses the default setting of printing the value of the variable after each step.<br><br>(gdb) display sum<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Breakpoints\"><\/span><strong>Setting Breakpoints<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104.png\" alt=\"GDB terminal showing a breakpoint set at the start of the main function using the &#039;break main&#039; command.\" class=\"wp-image-396031\" title=\"\" srcset=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104.png 1024w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104-300x300.png 300w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104-150x150.png 150w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104-768x768.png 768w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-104-500x500.png 500w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Breakpoints<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Breakpoints are points that you can insert in your code to tell GDB to pause when the program reaches the point. This gives you the ability to investigate the state of the program at certain stages and can thus assist you in determining where the issue is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>break<\/strong>: Place a breakpoint on a given line number or on a given function.<br><br>(gdb) break main<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will introduce a break point at the beginning of the main body.<br><strong>delete<\/strong>: Remove a breakpoint.<br><br>(gdb) delete<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>info breakpoints<\/strong>: List breakpoints.<br><br>(gdb) info breakpoints<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After you have a breakpoint, you can use the run command to start the program. It will stop at the breakpoint and will enable you to examine the status of the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tracing_Logic_Errors\"><\/span><strong>Tracing Logic Errors<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106.png\" alt=\"GDB terminal showing backtrace output after a segmentation fault, displaying the call stack and error location.\" class=\"wp-image-396033\" title=\"\" srcset=\"https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106.png 1024w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106-300x300.png 300w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106-150x150.png 150w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106-768x768.png 768w, https:\/\/dutable.com\/wp-content\/uploads\/2025\/12\/image-106-500x500.png 500w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Logic errors may be the cause when your program is not performing as you hoped. GDB enables you to follow the execution of your program and it therefore becomes easier to identify the points of failure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>backtrace<\/strong>: This command will help you to see the call chronology of the functions and go back to the point the program is presently in.<br><br>(gdb) backtrace<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>info locals<\/strong>: Display local variables in this function.<br><br>(gdb) info locals<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These are commands that enable you to recover the sequence of execution of the program and determine the logical errors that may not be easily discerned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Debugging_of_Advanced_C_Programs\"><\/span><strong>The Debugging of Advanced C Programs<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C programs that are more complicated, debugging may be more difficult. The following are further debugging hints for larger programs:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>With Multiple Files: Use GDB<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In case your program is distributed in several files, GDB can still debug the complete program, provided that these files are compiled with the -g flag.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Segmentation Faults:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Segmentation faults (segfaults) are not unusual in C programs, and particularly occur with pointers. In case GDB hits a segfault, it will simply halt the program. backtrace can then be used in following the fault.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(gdb) backtrace<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Watchpoints:<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To terminate the program when the value of a variable has changed, you can set a watchpoint:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(gdb) watch variable_name<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Knowing how to debug programs is one of the best skills that any programmer should have and GDB is a great tool that will enable you to acquire this skill. Having this guide in consideration, you should have no trouble breaking even in debugging your C programs in Unix systems. Be it a simple problem you need to solve, or a complex bug, the step-by-step execution, breakpoints, and inspection of variables will enable you to track and debug issues with accuracy with the help of GDB.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It will take time and practice until you can identify and fix bugs more effectively, until you are able to write a C code that is cleaner and more efficient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The skill of debugging is a very important skill that a programmer must develop. Regardless of whether you are working alone on a project, or you are part of a team, you need to be able to find and resolve any problem in your code quickly, and this is success. This article shall present GNU&#8230;<\/p>\n","protected":false},"author":1905,"featured_media":396034,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4577,12164],"tags":[29087,30614,30615],"class_list":["post-396027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-science-technology","category-technology","tag-c-programming","tag-gdb-debugging","tag-unix-programming"],"_links":{"self":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/users\/1905"}],"replies":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/comments?post=396027"}],"version-history":[{"count":2,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396027\/revisions"}],"predecessor-version":[{"id":396049,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396027\/revisions\/396049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media\/396034"}],"wp:attachment":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media?parent=396027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/categories?post=396027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/tags?post=396027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}