#### C programming debugging exercises assignments solutions

Advertisements 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.

#### 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;
}
``````

#### Write a function to count number of characters in a given string

###### Exercise source code.
Write a function to count number of characters in a given string
``````
#include <stdio.h>
void NumOfChars(char *s, int *);
int main() {
char str[] = "Test string";
int res;
NumOfChars(str,&res);
printf("Result is:%d\n", res);
}
void NumOfChars(char *s, int *r)
{
int i = 0;
while(s[i] != '\0')
{
*r = *r + 1;
i++;
}
}
``````
###### Problem to debug.
Output of the above debugging exercise is given below.
```\$ gcc CharCount.c
\$ ./a.out
Result is:66339
\$
```
This debugging exercise has one issue. In this program we are counting the number of characters present in string "Test string". So excluding null character it should return as 11. But it is returning 66339. Try to solve this exercise.
The solution for this debugging exercise is provided below.
``````
#include <stdio.h>
void NumOfChars(char *s, int *);
int main() {
char str[] = "Test string";
int res = 0;
NumOfChars(str,&res);
printf("Result is:%d\n", res);
}
void NumOfChars(char *s, int *r)
{
int i = 0;
while(s[i] != '\0')
{
*r = *r + 1;
i++;
}
}
``````

#### Write a function to count number of words present in a string

###### Exercise source code.
Program to write a function to count number of words present in a string
``````
#include <stdio.h>
void NumOfWords(char *s, int );
int main() {
char str[] = "Hello how are you";
int res = 1;
NumOfWords(str,res);
printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int r)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
r = r + 1;
}
i++;
}
}
``````
###### Problem to debug.
Output of the above debugging exercise is given below.
```\$ ./a.out
Result is:1
\$
```
This debugging exercise has one issue. We are expected to count the number of words present in the string str which is passed to the function NumOfWords. So as string str has 4 words we should get the count as 4. But we are getting the count as 1. Try to solve this exercise.
The solution for this debugging exercise is provided .

#### Write a function to loop through the strings present in an array of pointers to string

###### Exercise source code.
Write a function to loop through the strings present in an array of pointers to string
``````
#include <stdio.h>
char *StringSet[] = {
"string1 1 0 2 0",
"string2 1 2 0 5 9",
"string3 0 9 2",
""
};
int main(){
int i = 0;
while(StringSet[i] != NULL){
printf("String %s\n",StringSet[i]);
i++;
}
}
``````
###### Problem to debug.
Output of the above debugging exercise is given below.
```\$ ./a.out
String string1 1 0 2 0
String string2 1 2 0 5 9
String string3 0 9 2
String
\$
```
This debugging exercise has one issue. In this program we are expected to get only string1, string2, string3 as output. But we are getting inside the while loop 4 times as is observed in the program output. Try to solve this exercise.
The solution for this debugging exercise is provided .

Program debugging: Given program will not always give output as Office? Fix it
###### Exercise source code.
Program debugging: Given program will not always give output as Office? Fix it
``````
#include <stdio.h>
#include <string.h>
char strs[] = "Office-24423";
char strd;
int main() {
strncpy(strd, strs, 6);
printf("strd is %s\n",strd);
return 0;
}
``````
###### Problem to debug.
This debugging exercise has one issue. In this program we are copying 6 characters of source string strs to destination string strd. But the output will not always be Office as strncpy does not terminate the strd string with '\0' character after copying. Try to solve this exercise.
The solution for this debugging exercise is provided below.
``````
#include <stdio.h>
#include <string.h>
char strs[] = "Office-24423";
char strd;
int main() {
strd = '\0';
strncat(strd, strs, 6);
printf("strd is %s\n",strd);
return 0;
}
``````
Program debugging: Given program will not compile? Fix it
###### Exercise source code.
``````
#include <stdio.h>
int main() {
int result;
printf("%d\n", result);
return 0;
}
void addInt(int a, int b) {
result = a + b;
printf("Result:%d \n", result);
}
``````
The above program when compiled gives error as result as undeclared and will not compile. Try to fix it.
```\$ gcc prog.c
prog.c:10:4: error: ‘result’ undeclared (first use in this function)
result = a + b;
^~~~~~
prog.c:10:4: note: each undeclared identifier is reported only once for each function it appears in
\$
```
The solution for this exercise is given below.
``````
#include <stdio.h>
int result;
int main() {
printf("%d\n", result);
return 0;
}
void addInt(int a, int b) {
result = a + b;
printf("Result:%d \n", result);
}
``````

|
Share

#### C Programming #### Linux Software

 Linux important commands illustrations examples pthreads practical applications illustration in linux Cli development using ipc shared memory shmget Linux os concepts internals applications in a nutshell Process in Linux Shared library development with gcc in linux Writing practical makefiles linux examples illustrations Process practical applications illustration in linux #### Search Code Snippets

 Search more C examples Search more C MCQs

#### Popular C search MCQs  #### C Programming

 Variables, Variables scope, Datatypes Enums, Bit fiddling, practical applications Arrays, Two dimensional arrays Control flow, loops, operators, Macros Pointers and its practical applications Functions and its practical applications Data structures, practical applications Strings and its practical applications Structure, Unions, practical applications #### Python Programming 