Saturday, March 1, 2014

Security Tube GNU Debugger Expert Video Series

I have been viewing two videos of the GNU debugger expert series and found them easy to follow and practice. Even writing the source code by hand for the second video was fun as I have not touched C in a long time now. It has been so long that I forgot that the program need two command line arguments and got a segmentation fault when I ran the program without command line arguments.

Given below is the program.

#include<stdio.h>

int addnumbers(int num_1, int num_2)
{
   int sum = 0;
   sum = num_1 + num_2;
   return sum;
}

int subnumbers(int num_1, int num_2)
{

 int subtrt =0;
 subtrt = num_1 - num_2;
 return subtrt;
}

int main(int argc, char **argv)
{

 int num_1 = atoi(argv[1]);
 int num_2 = atoi(argv[2]);

 printf("\n\n Welcome to simple add and subtract program\n\n\n");
 printf("Sum of %d + %d = %d\n\n", num_1, num_2, addnumbers(num_1, num_2));
 printf("Difference of %d - %d = %d\n\n",num_1, num_2,subnumbers(num_1, num_2));
 getchar();
 return 0;
}

This elementary program does not have error checking which is why is crashes when run without command line arguments.

No comments:

Post a Comment