TU_Debugging_Exercise

Tutorial: Common Debugging Problems

Here is a list of common debugging mistakes that students make in C programming.

List of Common Mistakes

Basic Sytanx Error

  • Missing variable type declaration

  • Missing semicolon

  • Missing closing bracket in a conditional or loop statement

  • Format specifier error

  • More arguments than format specifiers

  • Type error in printf

Using Header File

  • Not Including header file

  • Include path error

  • Duplicate declaration of functions

Variable Type Usage

  • Dividing an interger

  • Multiplying an interger with a double(float) number

Array

  • Array size declaration

  • Array size overflow

  • Missing library declaration for dynamic memory allocation

Preparation

  1. Download the tutorial file and unzip.

  2. Copy Library Header File to include directory: .\NP\include\

    • NP_debugging_header.cpp, NP_debugging_header.h

    • Read here for detail:

  3. Create a new project under tutorial directory: .\NP\tutorial\

    • Project name: TU_Debugging

  4. Copy the source file under project directory: \NP\tutorial\TU_Debugging\

    • File: TU_Debugging_Examples.cpp

    • See Source File in Appendix

  5. Add the source file in Visual Studio '소스파일'

#include <stdio.h>
#include "../include/NP_debugging_header.h"

/////////////////////////////////////////////////////////
//	Function Declaration
/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
//	MAIN Function
/////////////////////////////////////////////////////////

int main() {
	int a = 1;
	int b = 2;
	int c = 3
		d = 4;

	int count = 0;

	for (int j = 0; j < 6; j++) {
		if (j == 5)
			printf("j is %d\n", j);

	printf("a= %d,  b= %f \n", a, b);
	printf("sum is %d\n", sum_int(a, b));
	printf("difference is %d\n", subtract_int(a, b));

	return 0;
}

/////////////////////////////////////////////////////////
//					Function Definition
/////////////////////////////////////////////////////////

// Sum of two inte

gers
int sum_int(int a, int b) {
	int sum = a + b;
	return sum;
}

Procedure

Run the program and fix all the debugging errors

You MUST read the error message first!!

Error: Header file include path

Error Message:

Error Reason:

  1. Wrong directory path

  2. Not included in V.Studio Project's include folder

Solution:

  • Fix include directory path

#include <stdio.h>
#include "../../include/NP_debugging_header.h"
  • Add in V.Studio Project's include folder

Error: Missing Semicolon & Variable Type

Error Message:

Error Reason:

  • Missing semicolon

  • Missing variable type

Solution:

  • add semicolon

  • include variable type

    	int a = 1;
    	int b = 2;
    	int c = 3;
    	int	d = 4;

Error: Missing Function Declaration

Error Message:

Error Reason:

  • Declaration of function missing: sum_int()

Solution:

  • Add function declaration before main(): if the function definition is located after main()

    /////////////////////////////////////////////////////////
    // Function Declaration
    /////////////////////////////////////////////////////////
    
    int sum_int(int a, int b);  // <--- declare before main(). 
    
    int main() {
    	int a = 1;

Now, find the rest of errors and fix them.


Assignment: Debugging

Due in 1 week.

Preparation

  1. Download the assignment file and unzip.

  2. Copy Library Header File to include directory: .\NP\include\

    • NP_debugging_header.cpp, NP_debugging_header.h

    • The same files used in the Tutorial.

  3. Create a new project under the assignment directory: .\NP\assignment\

    • Project name: Assignment_Debugging

  4. Copy the source file under the project directory: \NP\Assignment\Assignment_Debugging\

    • File: Assignment_Debugging.cpp

  5. Add the source file in the Visual Studio Project

Procedure

Download the report template

Run the code and read the error message first!!

Find all the errors and fix them.

You must write the report by including the following components

Error: Error Name

Error Message:

  • Show the error message

Error Reason:

  • Write the error reasons

Solution:

  • Show how you have fixed it

Last updated

Was this helpful?