0% found this document useful (0 votes)
9 views7 pages

Debugging Practice Sheet

The document is a practice sheet focused on debugging C code, highlighting common syntax and logical errors. It provides various code snippets with mistakes such as missing semicolons, incorrect format specifiers, and uninitialized variables, alongside corrected versions and explanations. The aim is to help learners identify and fix these errors before compiling their code.

Uploaded by

ichhoya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Debugging Practice Sheet

The document is a practice sheet focused on debugging C code, highlighting common syntax and logical errors. It provides various code snippets with mistakes such as missing semicolons, incorrect format specifiers, and uninitialized variables, alongside corrected versions and explanations. The aim is to help learners identify and fix these errors before compiling their code.

Uploaded by

ichhoya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Debugging and Common Mistakes

Practice Sheet
Identify and fix errors in each code snippet.
Abrar - CSE24

1. Input / Output and Syntax Mistakes


1. Missing Semicolon
1 # include <stdio .h>
2 int main(void) {
3 printf (" Hello , world !")
4 return 0;
5 }

2. Forgot to Include Header


1 int main(void) {
2 printf (" Test message \n");
3 return 0;
4 }

3. Missing Ampersand in scanf


1 # include <stdio .h>
2 int main(void) {
3 int age;
4 printf (" Enter age: ");
5 scanf ("%d", age);
6 printf (" Age = %d\n", age);
7 return 0;
8 }

4. Wrong Format Specifier


1 # include <stdio .h>
2 int main(void) {
3 float price;
4 printf (" Enter price : ");
5 scanf ("%d", & price );
6 printf (" Price = %f\n", price );
7 return 0;
8 }

5. Uninitialized Variable
1 # include <stdio .h>
2 int main(void) {

1
3 int n;
4 printf (" Value of n is %d\n", n);
5 return 0;
6 }

6. Assignment Instead of Comparison


1 # include <stdio .h>
2 int main(void) {
3 int a = 5;
4 if (a = 10)
5 printf (" Equal !\n");
6 else
7 printf (" Not equal !\n");
8 return 0;
9 }

7. Extra or Missing Braces


1 # include <stdio .h>
2 int main(void) {
3 int x = 3;
4 if (x > 0)
5 printf (" Positive \n");
6 printf (" Done\n");
7 return 0;
8 }

8. Missing Return Type


1 main () {
2 printf (" Hello \n");
3 }

2. Logic and Control Flow Errors


9. Extra Semicolon After Loop
1 # include <stdio .h>
2 int main(void) {
3 int i;
4 for (i = 0; i < 5; i++);
5 printf ("%d ", i);
6 return 0;
7 }

10. Forgetting Return Statement in Function


1 # include <stdio .h>
2 int add(int a, int b) {

2
3 int sum = a + b;
4 }
5 int main(void) {
6 int result = add (3, 4);
7 printf ("%d\n", result );
8 return 0;
9 }

11. Integer Division Trap


1 # include <stdio .h>
2 int main(void) {
3 int a = 7, b = 9;
4 float avg = (a + b) / 2;
5 printf (" Average = %.2f\n", avg);
6 return 0;
7 }

12. Wrong Loop Condition


1 # include <stdio .h>
2 int main(void) {
3 int i = 10;
4 while (i == 0) {
5 printf ("%d ", i);
6 i--;
7 }
8 return 0;
9 }

13. Incorrect Variable Scope


1 # include <stdio .h>
2 int main(void) {
3 for (int i = 0; i < 3; i++) {
4 int x = i;
5 }
6 printf ("%d\n", x);
7 return 0;
8 }

14. Missing else-if Structure


1 # include <stdio .h>
2 int main(void) {
3 int n = 5;
4 if (n > 0)
5 printf (" Positive \n");
6 if (n < 0)
7 printf (" Negative \n");
8 else
9 printf (" Zero\n");

3
10 return 0;
11 }

3. Arrays and Loops


15. Array Index Out of Bounds
1 # include <stdio .h>
2 int main(void) {
3 int arr [5] = {1 ,2 ,3 ,4 ,5};
4 printf ("%d\n", arr [5]);
5 return 0;
6 }

16. Off-by-One Error


1 # include <stdio .h>
2 int main(void) {
3 int i, arr [5] = {1 ,2 ,3 ,4 ,5};
4 for (i = 0; i <= 5; i++)
5 printf ("%d ", arr[i]);
6 return 0;
7 }

17. Wrong Array Initialization


1 # include <stdio .h>
2 int main(void) {
3 int arr [3];
4 arr = {1, 2, 3};
5 return 0;
6 }

18. Using Wrong Loop Variable


1 # include <stdio .h>
2 int main(void) {
3 int i, j;
4 for (i = 0; j < 5; i++)
5 printf ("%d ", i);
6 return 0;
7 }

4. Strings and Characters


19. String Input Error

4
1 # include <stdio .h>
2 int main(void) {
3 char name [10];
4 printf (" Enter name: ");
5 scanf ("%s", name [10]) ;
6 printf (" Hello %s\n", name);
7 return 0;
8 }

20. Forgetting Null Terminator


1 # include <stdio .h>
2 int main(void) {
3 char str [4] = {'H', 'i', '!'};
4 printf ("%s\n", str);
5 return 0;
6 }

21. Using gets()


1 # include <stdio .h>
2 int main(void) {
3 char s[20];
4 gets(s);
5 printf ("%s\n", s);
6 return 0;
7 }

5. Pointers and Functions


23. Uninitialized Pointer
1 # include <stdio .h>
2 int main(void) {
3 int *ptr;
4 *ptr = 10;
5 printf ("%d\n", *ptr);
6 return 0;
7 }

24. Returning Address of Local Variable


1 # include <stdio .h>
2 int* fun () {
3 int x = 10;
4 return &x;
5 }
6 int main(void) {
7 int *p = fun ();
8 printf ("%d\n", *p);

5
9 return 0;
10 }

25. Passing Wrong Argument Type


1 # include <stdio .h>
2 void printInt (int n) {
3 printf ("%d\n", n);
4 }
5 int main(void) {
6 float f = 3.5;
7 printInt (f);
8 return 0;
9 }

26. Dereferencing Null Pointer


1 # include <stdio .h>
2 int main(void) {
3 int *p = NULL;
4 printf ("%d\n", *p);
5 return 0;
6 }

27. Using Pointer After Free


1 # include <stdio .h>
2 # include <stdlib .h>
3 int main(void) {
4 int *p = malloc ( sizeof (int));
5 *p = 5;
6 free(p);
7 printf ("%d\n", *p);
8 return 0;
9 }

28. Missing Return Value in Non-void Function


1 # include <stdio .h>
2 int square (int n) {
3 n * n;
4 }
5 int main(void) {
6 int res = square (5);
7 printf ("%d\n", res);
8 return 0;
9 }

6
Answer Key (Corrected Code and Explanations)
Selected Examples
1. Missing Semicolon
1 # include <stdio.h>
2 int main(void) {
3 printf (" Hello , world !");
4 return 0;
5 }

3. Missing Ampersand in scanf


1 scanf ("%d", &age);

6. Assignment Instead of Comparison


1 if (a == 10)

9. Extra Semicolon After Loop


1 for (i = 0; i < 5; i++) {
2 printf ("%d ", i);
3 }

11. Integer Division Trap


1 float avg = (a + b) / 2.0;

15. Array Index Out of Bounds


1 printf ("%d\n", arr [4]);

20. String Input Error


1 scanf ("%9s", name);

23. Uninitialized Pointer


1 int a = 10;
2 int *ptr = &a;
3 printf ("%d\n", *ptr);

24. Returning Address of Local Variable


1 int* fun () {
2 static int x = 10;
3 return &x;
4 }

30. Missing Return in Non-void Function


1 int square (int n) {
2 return n * n;
3 }

End of Debugging Sheet. Practice identifying both syntax and logical bugs before compiling!

You might also like