- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Command Line Arguments in C
In any C program, there may be multiple functions, but the main() function remains the entry point from where the execution starts. While the other functions may have one or more arguments and a return type, the main() function is generally written with no arguments. The main() function also has a return value of "0".
int main() { ... ... return 0; }
Inside the main() function, there may be scanf() statements to let the user input certain values, which are then utilized.
#include <stdio.h> int main() { int a; scanf("%d", &a); printf("%d", a); return 0; }
What are Command Line Arguments?
Instead of invoking the input statement from inside the program, it is possible to pass data from the command line to the main() function when the program is executed. These values are called command line arguments.
Command line arguments are important for your program, especially when you want to control your program from outside, instead of hard coding those values inside the code.
Let us suppose you want to write a C program "hello.c" that prints a "hello" message for a user. Instead of reading the name from inside the program with scanf(), we wish to pass the name from the command line as follows −
C:\users\user>hello Prakash
The string will be used as an argument to the main() function and then the "Hello Prakash" message should be displayed.
argc and argv
To facilitate the main() function to accept arguments from the command line, you should define two arguments in the main() function – argc and argv[].
argc refers to the number of arguments passed and argv[] is a pointer array that points to each argument passed to the program.
int main(int argc, char *argv[]) { ... ... return 0; }
The argc argument should always be non-negative. The argv argument is an array of character pointers to all the arguments, argv[0] being the name of the program. After that till "argv [argc - 1]", every element is a command-line argument.
Open any text editor and save the following code as "hello.c" −
#include <stdio.h> int main (int argc, char * argv[]){ printf("Hello %s", argv[1]); return 0; }
The program is expected to fetch the name from argv[1] and use it in the printf() statement.
Instead of running the program from the Run menu of any IDE (such as VS code or CodeBlocks), compile it from the command line −
C:\Users\user>gcc -c hello.c -o hello.o
Build the executable −
C:\Users\user>gcc -o hello.exe hello.o
Pass the name as a command line argument −
C:\Users\user>hello Prakash Hello Prakash
If working on Linux, the compilation by default generates the object file as "a.out". We need to make it executable before running it by prefixing "./" to it.
$ chmod a+x a.o $ ./a.o Prakash
Example
Given below is a simple example that checks if there is any argument supplied from the command line and takes action accordingly −
#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"); } }
Output
When the above code is compiled and executed with a single argument, it produces the following output −
$./a.out testing The argument supplied is testing.
When the above code is compiled and executed with two arguments, it produces the following output −
$./a.out testing1 testing2 Too many arguments supplied.
When the above code is compiled and executed without passing any argument, it produces the following output −
$./a.out One argument expected
It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, then argc will be set at "1" and if you pass one argument, then argc is set at "2".
Passing Numeric Arguments from the Command Line
Let us write a C program that reads two command line arguments, and performs the addition of argv[1] and argv[2].
Example
Start by saving the code below −
#include <stdio.h> int main (int argc, char * argv[]) { int c = argv[1] + argv[2]; printf("addition: %d", c); return 0; }
Output
When we try to compile, you get the error message −
error: invalid operands to binary + (have 'char *' and 'char *') int c = argv[1]+argv[2]; ~~~~~~~~~~~~~~
This is because the "+" operator cannot have non-numeric operands.
The atoi() Function
To solve this issue, we need to use the library function atoi() that converts the string representation of a number to an integer.
Example
The following example shows how you can use the atoi() function in a C program −
#include <stdio.h> #include <stdlib.h> int main (int argc, char * argv[]) { int c = atoi(argv[1]) + atoi(argv[2]); printf("addition: %d", c); return 0; }
Output
Compile and build an executive from "add.c" and run from the command line, passing numeric arguments −
C:\Users\user>add 10 20 addition: 30
Example
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 (' ').
In this example, we will pass a command line argument enclosed inside double quotes −
#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"); } }
Output
When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output −
$./a.out "testing1 testing2" Program name ./a.out The argument supplied is testing1 testing2