O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.5. Using the Debugger

Sometimes your program won't work properly, and even after you spend hours staring at your source code it isn't obvious what has gone wrong. For times like these, Xcode has a built-in debugger. The debugger allows you to step through your code as the program is running and watch the state of your variables change.

3.5.1.

3.5.1.1. Try It Out: Debugging Changes to Calculator
  1. In Xcode, open your Calculator project that you saved earlier.

  2. Open the Build Results window.

  3. Make sure the Active Build Style popup reads Development; change it if necessary.

  4. Clean your build results by clicking the Clean All button.

  5. Close the Build Results window.

  6. Open Calculate.c.

  7. Make a copy of the second case statement at lines 23-25, and paste them back at line 26. The calculate function should look like the following code:

    int calculate(int a, int b, char operator)
    {
        int result;
    
        switch (operator) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '-':
                result = a - b;
                break;
            default:
                printf("unknown operator: %c\n", operator);
                exit(1);
        }
    
        return result;
    }
  8. Change case '-': at line 26 to case '*':.

  9. Save Calculate.c.

  10. Build and run your project.

  11. Enter 4 * 5. Calculator prints the following result. Obviously, something is wrong.

    Enter an expression: 4 * 5
    4 * 5 = −1
    
    Calculator has exited with status 0.
  12. Open main.c.

  13. Click in the gutter on the left side of the window at line 6, where the main function is defined. A marker appears there, as shown in Figure 3-18.

  14. Choose Build ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required