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[]) { /* … */ }
some facts on TCS programming.
- argv[0] contains the default value(generally 0) and not the input value, first value is stored in argv[1], 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, here we can ask user to input the 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.
Below are the most common questions asked for Command Line Arguments in TCS
- TCS Command Line Arguments – Fibonacci Series
- TCS Command Line Program to Swap two numbers
- TCS String Reversal Using Command Line Programming
- Greatest of Two Numbers using CLP
- LCM of Two Number using CLP
- Average of Two Numbers
- Sum of Digits of a number
- Area of a Triangle
- TCS Coding Questions – 1
- TCS Coding Questions – 2
- TCS Programming Questions – 4
- TCS Programming Questions – 5
- TCS Programming Questions – 6
- TCS Command Line Program – 7
- Checking Palindrome (Number)
- Checking Palindrome (String)
- Checking Prime or Not
- Reverse Digits of a Number
- TCS Coding Questions – 3
- Prime Sum in a Given Range
- TCS Array Ascending Descending
- Binary to Decimal
- Decimal to Binary
- Factorial of a Number
- Square Root of Prime Number
- Square Root without sqrt.h
- Armstrong Number
- Odd Even Number
- Binary to Octal
- Decimal to Octal
- Check Leap Year
- Area of Circle
- Area of Triangle
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;
}