Edit after editing the question: Also you're using argv[1]
before you checked argc
. This is an error.
If you pass no arguments, argc
is going to be 1
(because argv[0]
is usually the executable name).
So
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc<=1)
{
printf("You cannot have 0 parameters\n");
exit(255);
} else
{
int i;
int maxLength = 0;
const char* maxString = 0;
for(i = 1; i < argc; ++i)
{
// find out the length of the current argument
int length = strlen(argv[i]);
if(maxLength <= length)
{
maxLength = length;
maxString = argv[i];
}
}
printf("Largest string is %s\n", maxString);
}
} // main