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
    • 717
    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.

     

     

    1. #include <stdio.h>
    2.  
    3. int main( int argc, char *argv[] ) {
    4.  
    5. if( argc == 2 ) {
    6. printf("The argument supplied is %s\n", argv[1]);
    7. }
    8. else if( argc > 2 ) {
    9. printf("Too many arguments supplied.\n");
    10. }
    11. else {
    12. printf("One argument expected.\n");
    13. }
    14. }

     

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

     

    1. #include <stdio.h>
    2.  
    3. int main( int argc, char *argv[] ) {
    4.  
    5. printf("Program name %s\n", argv[0]);
    6. if( argc == 2 ) {
    7. printf("The argument supplied is %s\n", argv[1]);
    8. }
    9. else if( argc > 2 ) {
    10. printf("Too many arguments supplied.\n");
    11. }
    12. else {
    13. printf("One argument expected.\n");
    14. }
    15. }

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: