Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Command line argument in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 700
    Comment on it

    You can pass command line argument in C programming.

     

    The command line argument is handled by the program main function where argc[] is total number of  arguments passed, and argv[] is a array which points number of parameters passed in the program.

     

     

    #include <stdio.h>
    
    int main( int argc, char *argv[] )  {
    
       if( argc == 2 ) {
          printf("The argument supplied is %s\n", argv[1]);
       }
       else if( argc > 2 ) {
          printf("Too many arguments supplied.\n");
       }
       else {
          printf("One argument expected.\n");
       }
    }

     

    argv[] is the program name and argv[1] is like pointer to the command line, and *argv[n] is the last argument.

     

    #include <stdio.h>
    
    int main( int argc, char *argv[] )  {
    
       printf("Program name %s\n", argv[0]);
     
       if( argc == 2 ) {
          printf("The argument supplied is %s\n", argv[1]);
       }
       else if( argc > 2 ) {
          printf("Too many arguments supplied.\n");
       }
       else {
          printf("One argument expected.\n");
       }
    }

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: