Thread: debug a program

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    debug a program

    Can someone please explain me what debugging a program exactly means? and how to do it? I read articles where it says you write something like this --- $ cc program.c then $ a.out. What these mean? and where to write these commands? I'd be so thankful if someone took time to explain this to me.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,066
    Quote Originally Posted by lmanukyan
    Can someone please explain me what debugging a program exactly means? and how to do it?
    In general, "debugging a program" means finding and fixing the bugs in a program. There are many ways of doing this, and one of them involves using a tool called a "debugger", but a debugger is certainly not the only tool available.

    Quote Originally Posted by lmanukyan
    I read articles where it says you write something like this --- $ cc program.c then $ a.out. What these mean? and where to write these commands?
    The article author is referring to running commands at a command prompt, probably on a Unix or Unix-like system, though it could apply in other situations too. The "$" indicates a non-root command prompt, "cc program.c" is an example of invoking a compiler to compile "program.c", "$ a.out" is an example of running the program output by the compiler (i.e., "a.out" is a typical name for the executable program where no output filename was specified).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    You put breakpoints at the points you get wrong results and then you debug it with the red arrow, you can open the debugging windows to check every value that your variables get in each step to understand and find where it goes wrong, so you can fix it

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    The term 'debugging' came from the days when computers where the size of rooms and insects would get caught in the components and they would 'literally' have to debug the computer. The term just stuck!

    Today the term just means 'fixing an issue in a program’. typically, the code. for example, if I write and compile a C program and it crashes, or doesn't work as expected then it is said to have a bug, and is in need of debugging (the process of finding and fixing the issue(s)).

    There are three basic type of 'bugs' that a program can have, syntactical, semantic or logical.

    Syntax and semantics are two sides of the same coin.

    Almost every line in a source file is a command for the computer to execute (statements). The syntax and the semantics are instructions and the details of how to carry out those instructions.

    Code:
    int i = 5 / 2;
    the syntax is <type> identifier <assignment operator> <mathematical expression>

    In plan English, we want the computer to create an int object and assign the identifier direct access to the address, then we want the CPU to do the calculation of 5 / 2 and assign the result to the identifiers address.

    Syntactically and syntactically the statement is fine, provided we are only interested in the integer value, but what if when we divided 5 by 2 and we wanted 2.5? Suddenly, we have a semantic problem, we are expecting 2.5 and only getting 2 (due to truncating when dividing two int types).

    Now it's important to note, that only syntax errors will give us a compile error and is probably the easiest bug to get rid of because compilers will often point out the file name line and also underline the syntax error.

    Semantics is a little trickier, because the compiler assumes you understand that truncating occurs when dividing two integer types.

    Then there is the logical error, this is typically down to logical conditions in if, if else, for, while, do etc.. Something has gone wrong with a logical condition and the program has gone down the wrong branch in the code.

    Sometimes, It won’t be clear as to whether or not you have a semantic error or a logical one.

    let’s assume you have a huge project and let’s also assume that you do not understand what truncate is or does. (if you don’t, then even better).

    You write a basic program as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
               int i = 5 / 2;
    
               if ( i ==  2.5)
       {
               printf("I = 2.5\n");
               // Do stuff
       }
    
       return 0;
    }
    You need the ‘if conduction’ to evaluate to true. 5 / 2 is 2.5 right? But, you can’t figure out why the print statement inside the if condition isn’t displaying! Is this a logical error or semantics? How do I find out how to fix this bug!

    Well, I’d always start with adding a printf() debug statement before the if condition.

    now we have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
               int i = 5 / 2;
    
               printf("DEBUG : i's value is %d\n", i);
    
               if ( i ==  2.5)
       {
               printf("I = 2.5\n");
               // Do stuff
       }
    
       return 0;
    }
    output is:
    Code:
    Lawsons-iMac:my_c_projects lawson$ gcc problem.c
    Lawsons-iMac:my_c_projects lawson$ ./a.out
    DEBUG : i's value is 2
    Whoa? Why is it 2 and not 2.5? Let’s look on Google. “C dividing 5 by 2”, first search result: c - Why dividing two integers doesn't get a float? - Stack Overflow


    Ah-ha! I need to use a float, not an int, and I need to add .0 so 5.0 / 2.0!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
               float i = 5.0 / 2.0;
    
               printf("DEBUG : i's value is %d\n", i);
    
               if ( i ==  2.5)
       {
               printf("I = 2.5\n");
               // Do stuff
       }
    
       return 0;
    }

    output
    Code:
    problem.c:8:38: warning: format specifies type 'int' but the argument has type
         'float' [-Wformat]
           printf("DEBUG : i's value is %d\n", i);
                                       ~~     ^
                                       %f
    Ops, syntax error! Looks like the problem is in my problem.c source file on line 8! It is saying something about I’m formatting using an int but giving it a float. Oh, cool have a wiggly line under the problem part and a carrot (^) pointing to the issue, oh it has even given me what I could change it with. so %d need to be %f.

    final change.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
               float i = 5.0 / 2.0;
    
               printf("DEBUG : i's value is %f\n", i);
    
               if ( i ==  2.5)
       {
               printf("I = 2.5\n");
               // Do stuff
       }
    
       return 0;
    }

    output
    Code:
    DEBUG : i's value is 2.500000
    I = 2.5
    I hope this example helps you to understand about debugging. You're going to be doing a lot of it the more you program.

    The process I tend to follow is this:
    Find out what type of bug you're dealing with? Normally, i use printf()s throughout my code to keep an eye on what values are stored in a variable.
    Narrow it down and if I don't understand why it is happening it is time to hit up google to find out why I'm having this issue and get clues as to how to fix it.
    Once you isolate the issue, open up a new project and simplify the code until the issue is very clear.

    if you follow those steps, and you get really good at debugging. Sometimes it is enough to break out a pen and a piece of paper and jot down what you think might be the cause of the issue, followed by possible fixes, start there and systematically test your theories. This will make you much faster in solving and identifying problems.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Lawson should I put the breakpoints in front of the lines I assume are not correct? And where did you write these lines? I mean, gcc problem.c and a.out

    Lawsons-iMac:my_c_projects lawson$ gcc problem.c
    Lawsons-iMac:my_c_projects lawson$ ./a.out
    DEBUG : i's value is 2

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    659
    Quote Originally Posted by lmanukyan View Post
    Lawson should I put the breakpoints in front of the lines I assume are not correct? And where did you write these lines? I mean, gcc problem.c and a.out
    gdb is the debugger for the gcc compiler. If you use another compiler, then check with the documentation for that compiler for which debugger to use, and how.

    "Breakpoints", "Watchpoints", etc... are "Set" in the debugger program.

    See this tutorial for how to use the gdb debugger.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by lmanukyan View Post
    Lawson should I put the breakpoints in front of the lines I assume are not correct? And where did you write these lines? I mean, gcc problem.c and a.out

    Lawsons-iMac:my_c_projects lawson$ gcc problem.c
    Lawsons-iMac:my_c_projects lawson$ ./a.out
    DEBUG : i's value is 2
    I have a mac, which means I have access to a UNIX terminal. The terminal is similar to the windows 'cmd' or 'powershell', but work completely differently. The gcc and the ./a.out is the same thing as 'compile and run' button in most IDEs (interactive development environment). Typically, you'd use the terminal to compile C source files when or if you don't use an IDE (not sure that is an option on windows). if you are using windows, you probably have something like visual studio (if you don't, then you probably should).

    In visual studio the best way to debug is to create a break points by clicking slightly to the left side of the page line, a red dot should appear. Run the program, when it breaks you can press F11 or F10 to 'step through' your code, line by line. At the bottom you should see a window which displays all the variables values as you step through it.

    What OS do you use? and what IDE do you use?
    Last edited by Lawson; 12-20-2015 at 03:14 PM.

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Lawson I use Visual Studio 2012 on Windows 8.1. So, when I press F5 on Visual Studio, is it equal to "compile and run" and the gcc and a.out commands?

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by lmanukyan View Post
    Lawson I use Visual Studio 2012 on Windows 8.1. So, when I press F5 on Visual Studio, is it equal to "compile and run" and the gcc and a.out commands?
    gcc and a.out are unix terminal commands to compile a c program. Since you're using windows, you won't need to or have access to those commands (someone please correct me if I am wrong). So forget about gcc and a.out.. what I'm saying is when you hit F5 your IDE is doing that for you.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    Quote Originally Posted by Lawson View Post
    gcc and a.out are unix terminal commands to compile a c program. Since you're using windows, you won't need to or have access to those commands (someone please correct me if I am wrong). So forget about gcc and a.out.. what I'm saying is when you hit F5 your IDE is doing that for you.
    There are windows ports and installers like TDM-GCC, which installs the MinGW port.

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    So, pressing F5 = to debug the program?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Giwrgows x View Post
    You put breakpoints at the points you get wrong results and then you debug it with the red arrow, you can open the debugging windows to check every value that your variables get in each step to understand and find where it goes wrong, so you can fix it
    You must be referring to a specific debugger. Perhaps you should explain which program you're talking about.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    659
    Quote Originally Posted by Lawson View Post
    gcc and a.out are unix terminal commands to compile a c program. Since you're using windows, you won't need to or have access to those commands (someone please correct me if I am wrong). So forget about gcc and a.out.. what I'm saying is when you hit F5 your IDE is doing that for you.
    For the record:

    gcc is a CLI (Command Line Interface) compiler for Linux, UNIX, with installers for Windows, and Mac.

    "a.out" is the default executable output files from gcc, clang, and possibly other compilers.

    For example, if compiling the file, "sample.c", turning on high warning level, with the "-Wall" option:
    Code:
     gcc -Wall sample.c
    Will produce the executable output file, "a.out".

    Code:
    gcc -Wall -o sample sample.c
    Using the "-o" option, this will produce the executable output file, "sample".

  14. #14
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    yeah, really don't use windows. good to know you can though.

  15. #15
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by rstanley View Post
    For the record:

    gcc is a CLI (Command Line Interface) compiler for Linux, UNIX, with installers for Windows, and Mac.

    "a.out" is the default executable output files from gcc, clang, and possibly other compilers.

    For example, if compiling the file, "sample.c", turning on high warning level, with the "-Wall" option:
    Code:
     gcc -Wall sample.c
    Will produce the executable output file, "a.out".

    Code:
    gcc -Wall -o sample sample.c
    Using the "-o" option, this will produce the executable output file, "sample".
    Awesome, never compiled in a terminal before!

    Elkvis,
    His referring to visual studio.

    lmanukyan So, pressing F5 = to debug the program?
    Sure, if it breaks. You have a bug! Find it, fix it. gcc isn't 'debugging' anything, it is compiling.
    Last edited by Lawson; 12-21-2015 at 08:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me debug this program
    By haibuithe in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2012, 03:41 AM
  2. c program to debug
    By panks in forum C Programming
    Replies: 2
    Last Post: 03-28-2011, 09:47 AM
  3. Please debug this program
    By TheDeveloper in forum C Programming
    Replies: 9
    Last Post: 01-07-2009, 11:00 AM
  4. Would you like to debug this program?
    By Salem in forum Tech Board
    Replies: 4
    Last Post: 04-05-2007, 11:48 AM
  5. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM