Skip to main content

Command line arguments in c language

Hey Folks!

                In this blog i would like to give you some ideas about command line arguments in c language. Its actually easy and it will be very interesting to write a program in command line arguments.

Is command line argument is that much important?

               Yes, Recently while going through the top MNC's preliminary round which include a coding round where most of the questions' i.e, 75% of questions are write a program using command line arguments in  c language. Especially TCS, Capgemni, CTS, and some other companies are looking for the candidate whoever good at writing a good code with excellent logics and added to that their ability to write the code getting the input as command line arguments .

               From 2018 they have updated their questions pattern for the first round. For the past few year they have focus  on only the general aptitude round includes questions for testing verbal ability, Quantitative aptitude , Logical reasoning , and at last the programming test will be there for the people who are going to appear in up coming drive for Top MNCs' .

COMMAND LINE ARGUMENTS           

            To start write code with main as:
                                             
                                        int main( int argc, char* argv[])
                                                  {
                                                    //code
                                                         }


      Thinks to remember:

  •   .Arguments values starts from the array index of 1             argv[1]  
  •   Array index 0 contains the program name or file name.      argv[0]
  •    argv[argc] is a null pointer.
  •   atoi() is a method to use convert string into integer  eg: atoi(argv[2]);
  •  atoi() function will be available in the header stdlib . 
  •   argc      --------------   argument count
  •    argv[]   --------------   array of values
  • *argv[] --------------   Pointers, contains location of all the arguments

Here some sample programs using command line arguments which have been asking in many companies.



ADDITION OF TWO NUMBERS:

#include <stdio.h>

#include<stdlib.h>

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

int a,b,c;

a=atoi(a[1]);

b=atoi(b[2]);

c= a*b;

printf("%d",c);

  return 0;
}

AREA OF CIRCLE:

#include <stdio.h>

#include<stdlib.h>


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

{

int r;

float a;

r=atoi(argv[1]);

 a= 3.14*r*r;

printf("%.2f",a);

return 0;

}


FACTORIAL OF A NUMBER:

#include <stdio.h>

#include<stdlib.h>


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

{

 long f;

int n,i;

 n=atoi(argv[1]);

f=1;

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

f=f*i;

printf("%ld",f);

 return 0;
}

FIBONACCI SERIES:

#include <stdio.h>

#include<stdlib.h>


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

{
 int n;

 int a,b,c,i;

  n=atoi(argv[1]);

 a=-1;

 b=1;

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

   {

          c=a+b;

        printf("%d\t",c);

        a=b;

         b=c;

         }

return 0;
}

HYPOTENOUS:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{

 double hyp,opp,adj;
   
 opp=atoi(argv[1]);
   
 adj=atoi(argv[2]);
    
opp*=opp;
    
adj*=adj;
   
 hyp=sqrt(opp+adj);
    
printf("%lf\t",hyp);
        
return 0;
}


SQUARE OF A NUMBER:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{
 int a,sq;

 a=atoi(argv[1]);

 sq= a*a;
printf("%d",sq);
return(0);

}

PRIME NUMBER:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{
 int n;
int f=0,i;
n=atoi(argv[1]);

   for(i=2;i<=sqrt(n);i++)
  
if(n%i==0)
  
 f=1;

   break;

  }

   if(f==0)
   
printf("the given number %d is prime",n);
   
else
   
printf("the given number %d is not a prime",n);
    
return(0);
}

LEAP YEAR:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{
 int n;
 n=atoi(argv[1]);

 if(n%4==0)
  
printf("the given year %d is leap year",n);
  
else
  
printf("the given year %d is not a leap year",n);
    return(0);

}

AMSTRONG NUMBER:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{

 int n,m,s,r;
n=atoi(argv[1]);
m=n;

 s=0;
while(n!=0)

 {
    r=n%10;
  
   s=s+r*r*r;
    n=n/10;
     
   }
     
if(m==s)
     
printf("the given number %d is amstrong number",m);
     
else
     
printf("the given number %d is  not an amstrong number",m);
     
return(0);

}


ODD NUMBERS IN A DIGIT:


#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{

 int n,m,r,c;
 n=atoi(argv[1]);

 m=n;
 c=0;
while(n!=0)

 {
  
   r=n%10;
     
if(r%2!=0)
     
c++;
     
n=n/10;
  
       }
     
     printf("the given number %d has %d odd numbers",m,c);
     
return(0);

}


PALINDROME OR NOT:

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

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

{

 int n,m,s,r;
 n=atoi(argv[1]);
m=n;

 s=0;
while(n!=0)

 {
  r=n%10;
 s=s*10+r;
    n=n/10;
     
    }
     
if(m==s)
     
printf("the given number %d is palindrome",m);
     
else
     
printf("the given number %d is  not a palindrome",m);
     
return(0);
}

PROGRAM TO DISPLAY MULTIPLES OF 3 AND 4 IF COMMON DISPLAY *:

#include <stdio.h>

#include<stdlib.h>

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

{

 int i,n;
n=atoi(argv[1]);

 for(i=1;i<=n;i++)
if(i%3==0&&i%4==0)

    printf("*\t");
    
else if (i%3==0||i%4==0)
    
printf("%d\t",i);
    
return(0);

}

LENGTH OF STRING:

#include <stdio.h>

#include<stdlib.h>

int main(int argc, char* argv[])
{
int i,n;
char *c;

c=argv[1];
i=0;
n=0;
while(*c++!='\0')
  
             n++;

 printf("%d",n);
    
    return(0);
}

VOWELS CALCULATION IN  COMMAND LINE ARGUMENTS:

#include <stdio.h>

#include<stdlib.h>

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

 int i,n;
 char *c;

c=argv[1];
i=0;

 n=0;

 while(*c!='\0')
        {
     
            char x= tolower(*c++);
     
                 if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u')
     
                  n++;
   
  
         }
printf("%d",n);
    
    return(0);


}


STRING REVERSE:

#include<stdio.h>

#include<string.h>

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

   int i, j;
   /* Make sure correct command line arguments are passed */
  
 if(argc!=2)
{
      
 printf("Invalid Usage.\n");
       
printf("Usage Example: ./a.out string_to_reverse");
       
return 1;

   }
    
  
 str = argv[1]; // 2nd argument will the string to be reversed

   i = 0; // Initialize i at start 

   j = strlen(str) - 1; // Initialize j with end of string length
   /* Swap characters from start and end */
  while (i < j) 
{
    
  temp = str[i];
    
  str[i] = str[j];
   
   str[j] = temp;
     
 i++;
     
 j--;

   }
  
   
printf(str);
  
 return 0;

}

BIGGEST OF N NUMBERS:

#include<stdio.h>

#include<stdlib.h>

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

    
int arr[10];
    
int i,j;
    
int max;
   
 j-0;
  
  for(i=1;i<argc;i++)
    
       arr[j++]=atoi(argv[i]);
      max=arr[0];
    
for(i=0;i<j;i++)
    
if(arr[i]>max)
    
 max=arr[i];
   
    printf("%d",max);

  
 return 0;

}

LCM OF TWO NUMBERS:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

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

   
 int n1,n2,x,y;
  
if (argc == 1 || argc > 3)
 {
    
printf("Enter Two Number\r\n");
    
exit(0);
  
}

    x= atoi(argv[1]);
   
 y=atoi(argv[2]);
   
 n1=x;
  
  n2=y;
  
  while(n1!=n2)
   
 {
  
  if(n1>n2)
         n1=n1-n2;
    
else
   
       n2=n2-n1;
  
  }
    
printf("%d", x*y/n1);

  
 return 0;

}

AVERAGE OF GIVEN NUMBERS:

#include<stdio.h>

#include<stdlib.h>

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

int count;

float sum=0;

int i=1;
  
  if(argc==1)
    
{
      
  printf("enter two or more numbers to get average");
      
  exit(0);
  
  }
   count=argc-1;
   
 while(i<=count)
    
{
       
 sum+=atoi(argv[i++]);
   
 }

printf("%f",sum/count);
    
  
 return 0;

}

BINARY TO DECIMAL:

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

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

{
 int count;
int n,s,i,r,temp;

 n=atoi(argv[1]);

 s=0;
i=0;
temp=n;
while(n!=0)

 {
  
   r=n%10;
     
   s=s+r*pow(2,i++);
     
   n=n/10;

 }

 printf("Decimal value of %d is :%d",temp,s );

return 0;

 }
    


TIPS TO GET EFFICIENT OUTPUT IN COMMAND LINE ARGUMENT:

condition 1:
 To know whether they have given input or not

if(argc==1)
{
printf(" You have not yet given the arguments just give the arguments");
return -1;//to exit
}
else
{
//Lines of code
}

condition 2: 
To check whether the given arguments exceeds limit or incorrect argument counts

 if(argc == 1 ||  argc > 2)
    {
         printf("Enter the number\n");
         exit(1);
    }
else
{
//Lines of code
}

Condition X:
Like wise before writing the logic for the given problem we can check the argument status in order to avoid the confusions and improper answers.


Here i have given a  piece information about Writing program using input of command line arguments in c . If there is any chaos with above code and logic , please mention it in the command section . Surely i will work for it to clear .







Comments

Popular posts from this blog

Gist about GitHub

Hey folks,                     For Tech savvies this blog will be an entertainment page just enjoy, for people who are all seeking for good technical stuffs , this page would be a sneak peak of GitHub.  If you are a developer GitHub going to play a phenomenal role for you better future and make your works so simple.      what is GitHub?                              GitHub is a version controller repository .It allow you to track the history of collections of files It states that developers and programmer may come across lots of idea and their work portfolio. In order to preserve those collections they have to take a backup of it any drives but its actually a tedious process to follow and to remember the files details and related things, but GitHub helps to maintain the details of specific details and their versions.   ...

programming is not a big task...Its like gaming!!!

                          Computer programming is the craft of writing useful, maintainable, and extensible source code which can be interpreted or compiled by a computing system to perform a meaningful task.Programming is a way of thinking, not a rote skill. Learning about "for" loops is not learning to program, any more than learning about pencils is learning to draw.People understand what they can see. If a programmer cannot see what a program is doing, They can't understand it. The goals of a programming can be :    i)To support and encourage powerful ways of thinking    ii)To enable programmers to see and understand the execution of their programs      A live-coding Processing environment addresses neither of these goals. JavaScript and Processing are poorly-designed languages that support weak ways of thinking, and ignore decades of learning about learning. And live coding, ...

JAVA BEAN

                                               EXTENDED JAVA                                          I n java platform,Standard Edition or Java SE is a widely used platform for development and deployment of portable code for desktop. Java SE should be run each and every time we need to use it the code.Java SE  uses the object-oriented java programming language. It is part of the java software platform family .Java SE should be run each and every time we need to use it the code. Enterprise Edition or Java EE is the standard in community driven enterprise software .It is a widely used computing platform  for enterprise software.Java EE extends the Java platform, standard Edition providing an API for object-relational mapping,distributed and multi-tier ar...