Open In App

How to Write a Command Line Program in C?

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C.

How to Write a Command Line Program in C?

Command line arguments are passed to the main() function. For that purpose, the main function should have the following signature:

int main( int argc , char *argv[] ){
     // Write your code
}

Here, argc is the number of arguments, and argv[] is the argument in the form of an array of strings.

Note: The argv[0] always contains the source file name.

C Program to Pass Command Line Arguments

C




// Program to demonstrate Command line arguments in C
#include <stdio.h>
#include <stdlib.h>
  
int main(int argc, char* argv[])
{
    // code to calculate the sum of n numbers passed to CLA
    int i, sum = 0;
  
    // iterating the loop till the the number of
    // command-line arguments passed to the program
    // starting from index 1
    for (i = 1; i < argc; i++) {
        sum = sum + atoi(argv[i]);
    }
    printf("Sum of %d numbers is : %d\n", argc - 1, sum);
    
    return 0;
}


Command Line Instruction

// assume that the file name is solution
./solution 10 20 30 40 50

Output

SUM of 5 numbers is: 150

Note: The command line arguments are separated by space so if you want to pass a space-separated string as a command line argument, then enclose them inside the “”.

To know more, refer to the article – Command Line Arguments in C


Similar Reads

C program to print odd line contents of a File followed by even line content
Pre-requisite: Basics of File Handling in C Given a text file in a directory, the task is to print all the odd line content of the file first then print all the even line content. Examples: Input: file1.txt: Welcome to GeeksforGeeks Output: Odd line contents: Welcome GeeksforGeeks Even line contents: to Input: file1.txt: 1. This is Line1. 2. This i
2 min read
Command line arguments example in C
Prerequisite: Command_line_argument. The problem is to find the largest integer among the three using command line arguments. Notes: Command-line arguments are given after the name of the program in the command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments: the first argument is the n
3 min read
How to create a command-line progress bar in C/C++
The task is to write a C/C++ program to draw a command-line progress bar. Approach: To create a progress bar the idea is to use system() function which will give colored output. Below is the illustration of how to use system() function. The system function accepts the following parameters for colouring the output screen: keyword: colorBackground co
2 min read
Command Line Arguments in C
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
4 min read
Write a one line C function to round floating point numbers
Algorithm: roundNo(num) 1. If num is positive then add 0.5. 2. Else subtract 0.5. 3. Type cast the result to int and return. Example: num = 1.67, (int) num + 0.5 = (int)2.17 = 2 num = -1.67, (int) num - 0.5 = -(int)2.17 = -2 Implementation: /* Program for rounding floating point numbers */ # include&lt;stdio.h&gt; int roundNo(float num) { return nu
1 min read
Write one line functions for strcat() and strcmp()
Recursion can be used to do both tasks in one line. Below are one line implementations for stracat() and strcmp(). /* my_strcat(dest, src) copies data of src to dest. To do so, it first reaches end of the string dest using recursive calls my_strcat(++dest, src). Once end of dest is reached, data is copied using (*dest++ = *src++)? my_strcat(dest, s
2 min read
C program to write an image in PGM format
PGM stands for Portable Gray Map. Saving a 2D array in C as images in PNG, JPG or other formats would need a lot of effort to encode the data in the specified format before writing to a file. However, Netpbm format offers a simple solution with easy portability. A Netpbm format is any graphics format used and defined by the Netpbm project. The port
4 min read
C Program To Write Your Own atoi()
The atoi() function in C takes a string (which represents an integer) as an argument and returns its value of type int. So basically the function is used to convert a string argument to an integer. Syntax: int atoi(const char strn) Parameters: The function accepts one parameter strn which refers to the string argument that is needed to be converted
5 min read
Write a C program to print "Geeks for Geeks" without using a semicolon
First of all we have to understand how printf() function works. Prototype of printf() function is: int printf( const char *format , ...) Parameter format: This is a string that contains a text to be written to stdout.Additional arguments: ... (Three dots are called ellipses) which indicates the variable number of arguments depending upon the format
2 min read
Write a C program that does not terminate when Ctrl+C is pressed
Write a C program that doesn't terminate when Ctrl+C is pressed. It prints a message "Cannot be terminated using Ctrl+c" and continues execution. We can use signal handling in C for this. When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.
2 min read