C programming debugging exercises assignments solutions

Share

Article background

When you are working on a c programming project and implementing different functionality as per the requirements of the project you will come accross issues during during source code development phase or integration testing phase or sytem test or field trials phase. So if you have good debugging skills that will help you to resolve the errors in your code and put your project in a clean state.

In this article we will help you to develop those debugging skills by using which you can write better code during development phase or resolve issues during integration/system level testing or field trials. I will give you here small code snippets having issues in it as a debugging exercise and you will try those execises to resolve the issue. I will also provide a link to the solution of each of the debugging exercise to verify your solution. Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs Course description

Learn, test your C programming skills and earn certificate for software jobs Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs

Write a function to count the sum of all the digits present in an integer number

Exercise source code.
Write a function to count the sum of all the digits present in an integer number

#include <stdio.h>
int SumDigit(int );
int main()
{
int res;
res = SumDigit(1223);
printf("The sum of digits %d\n", res);
return 0;
}
int SumDigit(int num)
{
int sum = 0,rem;
while(num > 9)
{
rem = num%10;
num /= 10;
}
sum += num;
return sum;
}
Problem to debug.
Output of the above debugging exercise is given below.
\$ ./a.out
The sum of digits 1
\$
This debugging exercise has one issue. In this program we are counting the sum of all digits present in number 1223. So it should be 1+2+2+3 i.e. 8. But we are getting program output as 1. Try to solve this exercise.
The solution for this debugging exercise is provided below.

#include <stdio.h>
int SumDigit(int );
int main()
{
int res;
res = SumDigit(1223);
printf("The sum of digits %d\n", res);
return 0;
}
int SumDigit(int num)
{
int sum = 0,rem;
while(num > 9)
{
rem = num%10;
sum += rem;
num /= 10;
}
sum += num;
return sum;
}