Site icon DataFlair

Command Line Arguments in C – Don’t be Confused be Practical!

Command Line Arguments in C

So far, we have seen that no arguments were passed in the main function. But the C programming language gives the programmer the provision to add parameters or arguments inside the main function to reduce the length of the code. These arguments are called command line arguments in C.

In this tutorial, we will discuss:

What are Command Line Arguments in C?

Command line arguments are nothing but simply arguments that are specified after the name of the program in the system’s command line, and these argument values are passed on to your program during program execution.

Components of Line Arguments

In order to implement command line arguments, generally, 2 parameters are passed into the main function:

  1. Number of command line arguments
  2. The list of command line arguments

The basic syntax is:

int main( int argc, char *argv[] )
{
.
.
// BODY OF THE MAIN FUNCTION
.
.
}

Another way to implement command line arguments is:

int main( int argc, char **argv[] )
{
.
.
// BODY OF THE MAIN FUNCTION
.
.
}

Now, let us discuss the components of this code in detail:

C Program to Understand the Command Line Arguments in C

Here is a code in C that illustrates the use of command line arguments.

// The program name is cl.c
#include<stdio.h>
int main(int argc, char** argv)
{

printf("Welcome to DataFlair tutorials!\n\n");

int i;
printf("The number of arguments are: %d\n",argc);
printf("The arguments are:");

for ( i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}
return 0;
}

If you’re still having any doubts related to the above syntax then don’t forget to check this

Quiz on Command Line Arguments in C

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc)

    {

    cout <<argc<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<argc<< “\n”;

    return 0;

    }

     

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<argv<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<argv[0]<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<“argument:”<<argv[1]<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<“argument:”<<argv[argc]<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout << argc<< ” “;

     

    for (int i = 0; i < argc; ++i)

         cout << argv[i];

     

    return 0;

    }

     

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <iostream>

    using namespace std;

     

    int main(int argc, char** argv)

    {

    cout <<argv[2]<< “\n”;

    return 0;

    }

     

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include <iostream>

    using namespace std;

     

    int main()

    {

    cout <<argc<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <iostream>

    using namespace std;

     

    int main(char** argv)

    {

    cout <<argv[0]<< “\n”;

    return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    what is the first argument in command line arguments?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    what is an argument vector?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    what is an argument count?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

     what does argv[0] holds?

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    what does argv[1] holds?

    Correct
    Incorrect

Summary

In this tutorial, we understood the basic meaning behind command line arguments and its 2 components.

We inferred that one is used to find the number of command line arguments and the other is used to find the list of command line arguments such as the program name.

Feel free to leave a comment below so that we could better understand your responses.

Exit mobile version