Command line arguments example in C
Prerequisite: Command_line_argument.
The problem is to find the largest integers among three using command line argument.
Notes:
int main(int argc, char *argv[]) { /* ... */ }
Examples:
Input : filename 8 9 45 Output : 45 is largest Input : filename 8 9 9 Output : Two equal number entered Input : filename 8 -9 9 Output : negative number entered
During execution of program, we pass three integers along with filename of the program and then we will find the largest numbers among three.
Approach :
- The program “return 1” if any of the two conditions satisfy:
- If any two numbers are same, print the statement “two equal numbers entered”.
- If any number is negative, print “negative number entered”.
- Else “return 0” if three different integers are entered.
For better understanding run this code on your linux machine.
// C program for finding the largest integer // among three numbers using command line arguments #include<stdio.h> // Taking argument as command line int main(int argc, char *argv[]) { int a, b, c; // Checking if number of argument is // equal to 4 or not. if (argc < 4 || argc > 5) { printf("enter 4 arguments only eg.\"filename arg1 arg2 arg3!!\""); return 0; } // Converting string type to integer type // using function "atoi( argument)" a = atoi(argv[1]); b = atoi(argv[2]); c = atoi(argv[3]); // Checking if all the numbers are positive of not if (a < 0 || b < 0 || c < 0) { printf("enter only positive values in arguments !!"); return 1; } // Checking if all the numbers are different or not if (!(a != b && b != c && a != c)) { printf("please enter three different value "); return 1; } else { // Checking condition for "a" to be largest if (a > b && a > c) printf("%d is largest", a); // Checking condition for "b" to be largest else if (b > c && b > a) printf ("%d is largest", b); // Checking condition for "c" to be largest.. else if (c > a && c > b) printf("%d is largest ",c); } return 0; } |
chevron_right
filter_none
Output :

![]()
![]()
Recommended Posts:
- Command line arguments in C/C++
- getopt() function in C to parse command line arguments
- Program to delete a line given the line number from a file
- Templates and Default Arguments
- Default arguments and virtual function
- How to Count Variable Numbers of Arguments in C?
- When do we pass arguments by reference or pointer?
- Variable length arguments for Macros
- Some Interesting facts about default arguments in C++
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 2
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- Line Splicing in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



