Command Line Arguments in C - GeeksforGeeks (2024)

Last Updated : 09 Jan, 2024

Summarize

Comments

Improve

The most important function of C is the main() function. It is mostly defined with a return type of int and without parameters.

int main() { ... }

We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell of Operating Systems. Command-line arguments are handled by the main() function of a C program.

To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments and the second is a list of command-line arguments.

Syntax

int main(int argc, char *argv[]) { /* ... */ } orint main(int argc, char **argv) { /* ... */ }

Here,

  • argc (ARGument Count) is an integer variable that stores the number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, the value of argc would be 2 (one for argument and one for program name)
  • The value of argc should be non-negative.
  • argv (ARGument Vector) is an array of character pointers listing all the arguments.
  • If argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain pointers to strings.
  • argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.

For better understanding run this code on your Linux machine.

Example

The below example illustrates the printing of command line arguments.

C

// C program named mainreturn.c to demonstrate the working

// of command line arguement

#include <stdio.h>

// defining main with arguments

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

{

printf("You have entered %d arguments:\n", argc);

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

printf("%s\n", argv[i]);

}

return 0;

}

Output

You have entered 4 arguments:./maingeeksforgeeks

for Terminal Input

$ g++ mainreturn.cpp -o main $ ./main geeks for geeks

Note: Other platform-dependent formats are also allowed by the C standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program’s environment, otherwise accessible through getenv in stdlib.h. Refer C program to print environment variables for details.

Properties of Command Line Arguments in C

  1. They are passed to the main() function.
  2. They are parameters/arguments supplied to the program when it is invoked.
  3. They are used to control programs from outside instead of hard coding those values inside the code.
  4. argv[argc] is a NULL pointer.
  5. argv[0] holds the name of the program.
  6. argv[1] points to the first command line argument and argv[argc-1] points to the last argument.

Note: You pass all the command line arguments separated by a space, but if the argument itself has a space, then you can pass such arguments by putting them inside double quotes “” or single quotes ”.

Example

The below program demonstrates the working of command line arguments.

C

// C program to illustrate

// command line arguments

#include <stdio.h>

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

{

printf("Program name is: %s", argv[0]);

if (argc == 1)

printf("\nNo Extra Command Line Argument Passed "

"Other Than Program Name");

if (argc >= 2) {

printf("\nNumber Of Arguments Passed: %d", argc);

printf("\n----Following Are The Command Line "

"Arguments Passed----");

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

printf("\nargv[%d]: %s", i, argv[i]);

}

return 0;

}

Output in different scenarios:

1. Without argument: When the above code is compiled and executed without passing any argument, it produces the following output.

Terminal Input

$ ./a.out

Output

Program Name Is: ./a.outNo Extra Command Line Argument Passed Other Than Program Name

2. Three arguments: When the above code is compiled and executed with three arguments, it produces the following output.

Terminal Input

$ ./a.out First Second Third

Output

Program Name Is: ./a.outNumber Of Arguments Passed: 4----Following Are The Command Line Arguments Passed----argv[0]: ./a.outargv[1]: Firstargv[2]: Secondargv[3]: Third

3. Single Argument: When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output.

Terminal Input

$ ./a.out "First Second Third"

Output

Program Name Is: ./a.outNumber Of Arguments Passed: 2----Following Are The Command Line Arguments Passed----argv[0]: ./a.outargv[1]: First Second Third

4. A single argument in quotes separated by space: When the above code is compiled and executed with a single argument separated by space but inside single quotes, it produces the following output.

Terminal Input

$ ./a.out 'First Second Third'

Output

Program Name Is: ./a.outNumber Of Arguments Passed: 2----Following Are The Command Line Arguments Passed----argv[0]: ./a.outargv[1]: First Second Third


K

Kartik Ahuja and Avadhut Patade

Command Line Arguments in C - GeeksforGeeks (1)

Improve

Next Article

Variable Length Argument in C

Please Login to comment...

Command Line Arguments in C - GeeksforGeeks (2024)
Top Articles
Cryptosporidiosis
Term to Maturity in Bonds: Overview and Examples
Unit 30 Quiz: Idioms And Pronunciation
Restaurer Triple Vitrage
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
Dollywood's Smoky Mountain Christmas - Pigeon Forge, TN
Dew Acuity
Top Scorers Transfermarkt
Phone Number For Walmart Automotive Department
Call Follower Osrs
27 Places With The Absolute Best Pizza In NYC
Noaa Weather Philadelphia
Produzione mondiale di vino
Music Archives | Hotel Grand Bach - Hotel GrandBach
Monticello Culver's Flavor Of The Day
Bill Devane Obituary
Lantana Blocc Compton Crips
Thayer Rasmussen Cause Of Death
Brutál jó vegán torta! – Kókusz-málna-csoki trió
Premier Reward Token Rs3
Cvs Appointment For Booster Shot
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Everything We Know About Gladiator 2
Vipleaguenba
Lcwc 911 Live Incident List Live Status
Nordstrom Rack Glendale Photos
Walgreens Tanque Verde And Catalina Hwy
Gopher Hockey Forum
Ubg98.Github.io Unblocked
Is A Daytona Faster Than A Scat Pack
Diakimeko Leaks
Bjerrum difference plots - Big Chemical Encyclopedia
Costco Gas Hours St Cloud Mn
Ihub Fnma Message Board
Sadie Sink Reveals She Struggles With Imposter Syndrome
Craigslist Comes Clean: No More 'Adult Services,' Ever
Amazing Lash Bay Colony
Rogold Extension
Missing 2023 Showtimes Near Mjr Southgate
Vlocity Clm
The Ultimate Guide to Obtaining Bark in Conan Exiles: Tips and Tricks for the Best Results
Kokomo Mugshots Busted
24 slang words teens and Gen Zers are using in 2020, and what they really mean
Rogers Centre is getting a $300M reno. Here's what the Blue Jays ballpark will look like | CBC News
Scarlet Maiden F95Zone
Craigslist Woodward
Kaamel Hasaun Wikipedia
Walmart Listings Near Me
Mytmoclaim Tracking
10 Best Tips To Implement Successful App Store Optimization in 2024
WHAT WE CAN DO | Arizona Tile
One Facing Life Maybe Crossword
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 6167

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.