Command Line Arguments Questions for TCS

TCS Command Line Arguments Programs are a little different from basic C Programming. The main program while writing in basic C looks like-

int main()

// code written on some basic algorithm in TCS

}}

But while in command line arguments we write it like –

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

1.argc – It is called Argument Count and as the name states , it function is to store the Count of number of Arguments.
2.argv[] – It is the Pointer which contains location of all the values(arguments).
3.*argv[] – It is the Array of values of all the arguments.
They are the different parameters/arguments which are supplied to the program when it is invoked.

TCS COMMAND LINE ARGUMENTs have a couple of things from compiler point of view

1.Total Count of number of Arguments.
2.All the values/pointer location of arguments stored in an array.

Another is atoi();

atoi(); – It converts string into int and atoi(argv[i]); will give the value of argument at ‘i’th location in int type format.
So we can use an int val = atoi(argv[i]); to store the value and then this is printed with printf(); function.

To pass Command Line Arguments in TCS program, main() is defined with two arguments
a.the number of command line arguments
b.list of command-line arguments.

int main(int argc, char *argv[]) { /* … */ }
or
int main(int argc, char **argv[]) { /* … */ }

Now some facts on TCS programming.

argv[0] contains the default value and not the input value, thus –

All for loops must start from i = 1.
The following condition must be used
if(argc == 1){
// do nothing since, there are no arguments, maybe ask for arguments?
}else{
// code to apply logic and print values.
}
provided+1 +1 for file.exe
argv[argc] is a NULL pointer.
argv[0] holds the name of the program.
argv[1] points to the first command line argument and argv[n] points last argument.

We have posted questions on Command Line Arguments for TCS. Do go through them will preparing for TCS.

For more on TCS you can have a look at our TCS Main Dashboard  for Questions asked in TCS Placements where we have TCS Previous Year Placements Papers and TCS Placement Papers.

Program to print Fibonacci Series
#include
int main(int argc, char *argv[])
{
             int n, first = 0, second = 1, next, c;
            n = atol(argv[1]);
            printf("First %d terms of Fibonacci series are :-\n",n);
            for ( c = 0 ; c < n ; c++ )
   {
             if ( c <= 1 )
               next = c;
            else
      {
             next = first + second;
             first = second;
               second = next;
      }
            printf("%d\n",next);
   }
   return 0;
}
Program to find the Greatest Number
#include

int main(int argc, char *argv[])

{

            int c[10];

            int i,temp,j,greatest;

            j = 0;

            for(i=1; i<argc; i++)

{

            temp = atoi(argv[i]);

            c[j] = temp;

            j++;

}

            greatest = c[0];

            for (i = 0; i < 10; i++) {

            if (c[i] > greatest) {

            greatest = c[i];

               }

    }

            printf(“Greatest of ten numbers is %d”, greatest);

            return 0;

}

 
Program to print n factorial for a number n
#include

int main(int argc, char *argv[])

{

              int n,i;

            unsigned long long factorial = 1;

              n = atol(argv[1]);

              for(i=1; i<=n; ++i)

   {

            factorial *= i;

   }

            printf("Factorial of %d = %llu", n, factorial);

}
Program to Reverse a String
#include

int main(int argc, char *argv[])

{

            int k;

               char temp;

              int i,j=0;

              int strsize = 0;

               for (i=1; i<argc; i++) {

       strsize += strlen(argv[i]);

        if (argc > i+1)

            strsize++;

   }

   char *cmdstring;

   cmdstring = malloc(strsize);

   cmdstring[0] = '\0';

   for (k=1; k<argc; k++) {

        strcat(cmdstring, argv[k]);

        if (argc > k+1)

            strcat(cmdstring, " ");

   }

   i = 0;

   j = strlen(cmdstring) - 1;

   while (i < j) {

      temp = cmdstring[i];

      cmdstring[i] = cmdstring[j];

      cmdstring[j] = temp;

      i++;

      j--;

   }

   printf("\nReverse string is :%s", cmdstring);

   return(0);

}
Program to Swap to Numbers
#include

int main(int argc, char *argv[])

{

            double firstNumber, secondNumber, temporaryVariable;

              firstNumber = atol(argv[1]);

              secondNumber = atol(argv[2]);

              temporaryVariable = firstNumber;

              firstNumber = secondNumber;

            secondNumber = temporaryVariable;

              printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);

            printf("After swapping, secondNumber = %.2lf", secondNumber);

   return 0;

}

Tcs Command Line Arguments Programs

#include

int main(int argc, char *argv[])

{

            int radius;

             float area;

              radius = atol(argv[1]);

              area = 3.14 * radius * radius;

            printf("%f",area);

}
Find area of Triangle

#include

int main(int argc, char *argv[])

{

             int base, height;

            float area;

             base = atol(argv[1]);

            height = atol(argv[2]);

            area = base*height/2;

            printf("%f",area);

}

Checking if a number is Palindrome or not

#include

int main(int argc, char *argv[])

{

            int num, reverse_num=0,remainder,temp;

             num = atol(argv[1]);

            temp=num;

             while(temp!=0)

   {

            remainder=temp%10;

            reverse_num=reverse_num*10+remainder;

             temp/=10;

   }

   if(reverse_num==num)

            printf("%d is a palindrome number",num);

   else

            printf("%d is not a palindrome number",num);

   return 0;

}
Checking if the number is Prime or not

 #include

int main(int argc, char *argv[])

{

             int n, i, flag = 0;

             n = atol(argv[1]);

            for(i=2; i<=n/2; ++i)

   {

            if(n%i==0)

      {

            flag=1;

            break;

      }

   }

   if (flag==0)

            printf("%d is a prime number.",n);

   else

            printf("%d is not a prime number.",n);

   return 0;

}
#include

void isPalindrome(char str[])

{

             int l = 0;

            int h = strlen(str) - 1;

            while (h > l)

    {

            if (str[l++] != str[h--])

        {

                        printf("%s is Not Palindromen", str);

            return;

        }

    }

            printf("%s is palindromen", str);

}

int main(int argc, char *argv[])

{

            int i,k;

            int strsize = 0;

    for (i=1; i<argc; i++) {

         strsize += strlen(argv[i]);

    if (argc > i+1)

             strsize++;

    }

    char *cmdstring;

    cmdstring = malloc(strsize);

    cmdstring[0] = '\0';

    for (k=1; k<argc; k++) {

         strcat(cmdstring, argv[k]);

         if (argc > k+1)

             strcat(cmdstring, " ");

    }

    isPalindrome(cmdstring);

}

Checking if a number is Even or Odd

#include

int main(int argc, char *argv[])

{

            int number;

            number = atol(argv[1]);

   if(number % 2 == 0)

            printf("%d is even.", number);

   else

             printf("%d is odd.", number);

   return 0;

}