Implementing ‘strncmp( )’ by passing string as command line arguments


Command line arguments:

In C language main() is the only function which can defined in multiple ways.It can recieve no arguments, two arguments or 3 arguments. Using these arguments the main function will recieve the arguments from the console.
The main function can be define in the following ways:
1. int main( )
2. int main( int argc, char *argv[ ])

It is called as argument count ,it tells the number of arguments that is being passed to the main function.

argv[ ]:
This is called as argument vector ,its an array whose each element would be pointing to the arguments that are passed to the main function.

Implementing ‘strncmp(s2,s1)’:

Implementing ‘strncmp( )’ by passing string as command line arguments

strncmp( s1, s2, n) is a standard library function which compares the first ‘n’ characters of source s1 and target string s2.
If both the strings are equal the it returns a value ‘0’.
If the source string is greater than the target string it, returns a positive value .
If the source string is smaller than the target string ,it returns a negetive value .



We are taking the input from the user using command line arguments.
1.input = $ ./a.out Subhash Subhtadr 4
result = 0 ( Both the strings are equql)
result = 0 (i.e. the difference between ascii value of ‘a’ and ‘\0’).
2.input = $ ./a.out Subrat Subhash 4
result = -10 (i.e. Strings are not equal, argv[2] is higher than argv[1] ).
3.input = $ ./a.out Subhash Subcant 4
result = 3 (i.e. Strings are not equal, argv[1] is greater than argv[2] ).

In case of the second input given above “Subrath” is the source string , “Subhash” is the target string and 4 indicates how characters of the s1 has  to compared with s2. So argv[1] = “Subrat”, argv[2] = “Subhash” and argv[3] = 4. So strncmp( ) compares the first 4 characters of argv[1] and argv[2] ,in this case it returns positive value ie -10 ( difference between ‘h’ and ‘r’).



1.Take source , target strings and ‘n’ using command line arguments.
2.Call the function my_strncmp(argv[1] , argv[2], n ) and pass the base address of the argv[1] , argv[2] and n.
3.The control goes to the function defination( int my_strncmp(char * p, char * q, int n).
3.In the function defination consider a variable ‘i’ such that ‘i’ points to the first character in string s1 and s2, then continue comparision operation until( i < n) is satisfied.
5.If the above condion is true ,check if each character of the value to which p is pointing (*p) is not equal to the corresponding character of the value to which q is pointing( if(*p != *q)), if yes then return the difference between the ASCII value of those characters to the main else increment both the pointers ,’i’ and continue the loop.
6.If the above condition in 3 fails the control comes out of loop and returns ‘0’.

Source code:

int my_strncmp(char *,char *,int);
int main(int argc, char *argv[])
           int n = atoi(argv[3]);
          int r = my_strncmp(argv[1] , argv[2], n);
int my_strncmp(char *p, char * q, int n)
         int i;
         for(i = 0;i< n;p++,q++,i++)
                      if(*p != *q)
                            return (*p - *q);
          return 0;


This article is written by one of my student as part of their blog writing exercise.

To learn more programs on C and C++, click this link: Interview Questions

Subhash.K.U, Principal Mentor, Subhash Embedded Classes,Bangalore.



Subhash Embedded Classes is one of the Best Embedded Systems Training Institute in Bangalore

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.