
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Command line arguments example in C
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard-coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly −
Example Code
#include <stdio.h> int main( int argc, char *argv[] ) { if( argc == 2 ) { printf("The argument supplied is %s
", argv[1]); } else if( argc > 2 ) { printf("Too many arguments supplied.
"); } else { printf("One argument expected.
"); } }
Output
$./a.out testing The argument supplied is testing
Output
$./a.out testing1 testing2 Too many arguments supplied.
Output
$./a.out One argument expected
- Related Articles
- Command Line arguments in C#
- Command line arguments in C/C++
- Command Line Arguments in Python
- Command line arguments in Java
- Command Line arguments in Lua
- Java command line arguments
- How to Parse Command Line Arguments in C++?
- Explain Java command line arguments.
- getopt() function in C to parse command line arguments
- Command Line and Variable Arguments in Python?
- Command Line arguments in Java programming\n
- How command line arguments are passed in main method in C#?
- How to add command line arguments in Python?
- How do we access command line arguments in Python?
- How to pass command line arguments to a python Docker container?

Advertisements