TCS Previous Year Papers and Study Materials
TCS Previous Year Papers and Study Materials
Department of
COMPUTER SCIENCE & ENGINEERING
For TCS Command Line Arguments Programs let us consider this, if you wanted to
write a basic C program then you would’ve written a main function that would’ve
looked like –
int main(){
// some code in Command Line Arguments Questions for TCS C Compiler
}}
1. argc – It is know as Argument Count and as clear from the name it stores the
Count of number of Arguments.
2. argv[] – Pointer, contains location of all the values(arguments).
3. *argv[] – Array of values of all the arguments.
4. They are parameters/arguments supplied to the program when it is invoked.
atoi(); – Converts string into int and atoi(argv[i]); will give the value of
argument at ith location in int type format.
Now you can use an int val = atoi(argv[i]); to store the value and then print it with
printf(); function.
To pass command line arguments, we typically define main() with two arguments :
first argument is the number of command line arguments and second is list of
command-line arguments.
All for loops must start from i = 1.
You must use the following condition
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.
Command Line Arguments Questions for TCS Rules for Coding Section Steps:
There is only one question for 20 minutes.
We must only print exact output in Command Line Arguments Questions for TCS
Procedure –
Don’t Compile again and again since compiler takes 25 seconds and each time you
compile 25 seconds will become lesser in the time you have to code.
Example:
OutPut –
if(argc<2)
{
printf("please use \"prg_name value1 value2 ... \"\n");
return -1;
}
int a,b,side1,side2,side3;
a=atoi(argv[1]);
b=atoi(argv[2]);
side1=pow(a,2);
side2=pow(b,2);
side3=sqrt((side1+side2));
printf("the hypotenuse is %d",side3);
return 0;
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
int num=atoi(argv[1]);
if(isPalindrome(num))
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}
int isPalindrome(int n)
{
int m=n;
int rev=0;
while(m!=0)
{
rev=(rev*10) + (m%10);
m=m/10;
}
if(rev==n)
return 1;
else
return 0;
}
#include<stdio.h>
int main(int argc,char *argv[])
{
int N1,N2,i,j,sum=0,count,lower,upper;
if(argc!=3)
exit(0);
N1=atoi(argv[1]);
lower=N1+1;
N2=atoi(argv[2]);
upper=N2;
for(i=lower;i<upper;i++)
{
count=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==1)
{
sum=sum+i;
}
}
printf("%d",sum);
return 0;
}
String Reversal Program with Command Line Programming
It is highly advisable to go through Command Line Arguments before even
looking at the code. Here is the code for String Reversal Using Command Line
Arguments
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Input – abcde
Output – edcba
Binary to decimal using Command Line arguments
#include <stdio.h>
int main(int argc, char *argv[])
{
int num,binary,decimal=0,rem,base=1;
num=atoi(argv[1]);
binary=num;
while(num>0)
{
rem=num%2;
decimal+=rem*base;
num=num/10;
base=base*2;
}
printf("%d",decimal);
return 0;
}
int l = 0;
int h = strlen(str) - 1;
while (h > l)
if (str[l++] != str[h--])
return;
}
}
int i,k;
int strsize = 0;
strsize += strlen(argv[i]);
strsize++;
char *cmdstring;
cmdstring = malloc(strsize);
cmdstring[0] = '\0';
strcat(cmdstring, argv[k]);
isPalindrome(cmdstring);
}
Using Command Line Arguments Program to Convert a Decimal to
Binary Number
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No Arguments ");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
int binaryN[64];
int i=0;int j;
while(n>0)
{
//storing in binary array remainder of number
binaryN[i]=n%2;
n=n/2;
i++;
}
//printing reverse array
while(i)
{
printf("%d",binaryN[--i]);
}
return 0;
}
}