You can print the arguments without transferring them into character arrays. They are null-terminated C strings and printf eats them for breakfast:

for (i=0; i<argc; i++)
  printf("%s\n", argv[i]);  
Answer from David Heffernan on Stack Overflow
🌐
Quora
quora.com › What-is-string-args-in-C
What is string [] args in C
Answer (1 of 2): What is the use of the [code ]string [] args[/code] command in C#? [code ]string[] args[/code] isn’t a command, it is a declaration. It declares an array of [code ]string[/code] objects called [code ]args[/code]. That’s all it does. It is no different from any other variable ...
Discussions

Arguments to main in C - Stack Overflow
So if I want the programs name to be cp, do I initialize argv[0]="cp"; Cp being a string literal. And anything going to stdout goes on the command line??? I understood the example you gave! Even though the string you entered was a few words long, it was still only one arg. More on stackoverflow.com
🌐 stackoverflow.com
c# - Why using string[] args in all main methods? - Software Engineering Stack Exchange
I'm a beginner in learning programming. I ask about using the string array in main method as a parameter. Why not writing the Main() method without the string array? What is the point of having this More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
December 8, 2014
java - What is the "String[] args" parameter in the main method? - Stack Overflow
Source code and/or examples are preferred over abstract explanations. ... That String[] args part may become optional in future versions of Java. Work is underway to allow for simplified declaration of main method. See JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview), ... More on stackoverflow.com
🌐 stackoverflow.com
c# - What is "string[] args" in Main class for? - Stack Overflow
In C# the Main class has string[] args parameter. What is that for and where does it get used? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
pratikraj.medium.com › string-args-in-java-c-main-string-args-fd9792e9753d
String args in Java , C# →main(String[] args) | by Pratik Raj | Medium
February 24, 2020 - It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › command-line-arguments-in-c-cpp
Command Line Arguments in C - GeeksforGeeks
July 3, 2026 - argc stores the total number of command-line arguments, while argv is an array containing each argument as a string.
🌐
W3Schools
w3schools.com › c › c_functions_parameters.php
C Function Parameters
When the function is called, we ... Hello Liam // Hello Jenny // Hello Anja Try it Yourself » · The value that is passed to the function is called an argument....
Top answer
1 of 6
193

The signature of main is:

int main(int argc, char **argv);

argc refers to the number of command line arguments passed in, which includes the actual name of the program, as invoked by the user. argv contains the actual arguments, starting with index 1. Index 0 is the program name.

So, if you ran your program like this:

./program hello world

Then:

  • argc would be 3.
  • argv[0] would be "./program".
  • argv[1] would be "hello".
  • argv[2] would be "world".
2 of 6
11

Imagine it this way

*main() is also a function which is called by something else (like another FunctioN)

*the arguments to it is decided by the FunctioN

*the second argument is an array of strings

*the first argument is a number representing the number of strings

*do something with the strings

Maybe a example program woluld help.

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

    printf("you entered in reverse order:\n");

    while(argc--)
    {
        printf("%s\n",argv[argc]);
    }

return 0;
}

it just prints everything you enter as args in reverse order but YOU should make new programs that do something more useful.

compile it (as say hello) run it from the terminal with the arguments like

./hello am i here

then try to modify it so that it tries to check if two strings are reverses of each other or not then you will need to check if argc parameter is exactly three if anything else print an error

if(argc!=3)/*3 because even the executables name string is on argc*/
{
    printf("unexpected number of arguments\n");
    return -1;
}

then check if argv[2] is the reverse of argv[1] and print the result

./hello asdf fdsa

should output

they are exact reverses of each other

the best example is a file copy program try it it's like cp

cp file1 file2

cp is the first argument (argv[0] not argv[1]) and mostly you should ignore the first argument unless you need to reference or something

if you made the cp program you understood the main args really...

Find elsewhere
Top answer
1 of 3
19

Console applications predate GUI applications, and these take command-line parameters for very long time (at least from CP/M time, which preceded MS-DOS, which preceded Windows, which preceded Windows NT — all preserving more or less the same logic).

You can pass command-line parameters to Windows GUI apps, too.

These are the args.

2 of 3
7

This is not the sole province of "old" console (command line) applications. Every running program has a "command line" that points at the executable image and includes any command line parameters. Plenty of GUI apps take arguments that alter either their initialization behavior or runtime behavior or both.

Most languages do let you specify an entry-point function that takes no arguments. But virtually all of them also allow you to pass arguments. This args array is simply an ordered collection of whatever is passed on the command line after the name of the executable file when the program is executed. EDIT: And of course in the case of the C language (as one example), the first argument (index 0) is actually the name of the program.

For example, imagine a pointless little program that displays a message in a message box, and lets you specify the message and the title for the window on the command line, like this:

myprogram.exe -title Foo -message Bar

Your args array in this case will look like this, presuming a language like C# which does not include the program name in the args array:

args[ 0 ] = "-title"
args[ 1 ] = "Foo"
args[ 2 ] = "-message"
args[ 3 ] = "Bar"

Making sense of the order of the arguments, and which ones are valid or not, is totally up to your own application code.

🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › fundamentals › program-structure › main-command-line
Main() and command-line arguments - C# | Microsoft Learn
Include a string[] args parameter in your Main declaration to accept command-line arguments. If you don't need them, omit the parameter.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 113220-argv-string.html
Argv to string?
March 12, 2009 - I just didn't want to be fumbling with the argv thing...Seemed easier to copy it to its own string. If it's really that important to you, you could declare a new pointer and set it equal to argv[2], for instance. Since all of the string functions work by receiving the pointer instead of the array itself, that should be plenty.
🌐
Montana State University
montana.edu › rmaher › ee475_fl04 › Command_line.pdf pdf
Command-line arguments in the C language R.C. Maher EE475 Fall 2004
argv[0] is a pointer to the string “test_prog” ... The string functions atoi(), atol(), atof(), etc., will also work.
🌐
Jonathan Cook
cs.nmsu.edu › ~jcook › posts › c-cmdline-args
C/C++: Command Line Arguments • Jonathan Cook
August 26, 2024 - Each entry is a valid C string. All arguments are strings, even if they are numeric digits.
Top answer
1 of 3
6

In the last lines of your question, what you mention is C being a procedural language. This means, you should split your semantic logic into reusable smaller logical bits and implement smaller portions of your code and implement your program by using the functions you implemented.

Although, as you come from a object-oriented language, I believe you must be familiar with the concepts I mentioned.

Coming to your example, your program consists of a fairly small codebase and it does these things:

  • Iterate through the argv strings
  • Combine the argv strings in another string

But if I did not misunderstand, your first iteration is to find the count of the arguments. That is what argc variable, therefore your first while loop is unnecessary, you can simply set size = argc - 1;

You have an unnecessary condition check and this part is unnecessary:

strcat(str, argv[1]); // putting the string together
strcat(str, " ");
strcat(str, argv[2]);
strcat(str, " ");

You should start your for loop from 1 instead of 3, and remove these lines.

Your condition check also seems unnecessary, as your for for loop would produce similar results.

Therefore, I would not say that this code is bad due to not being written in procedural paradigm, but it is bad due to overall unnecessary lines of code.

A shorter approach would be:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if(argc == 1) {
        printf("ERROR: Expected at least 1 argument\n");
        return 0;
    }

    int i, v = 0, size = argc - 1;

    char *str = (char *)malloc(v);

    for(i = 1; i <= size; i++) {
        str = (char *)realloc(str, (v + strlen(argv[i])));
        strcat(str, argv[i]);
        strcat(str, " ");
    }

    printf("%s\n", str);
    return 0;
}
2 of 3
4
  • Trust the promise. argc is the Argument Count:

    int size = 0;
    while(argv[size]) { // gets the amount of arguments besides the first argument
        size++;
    }
    

    At this point you may assert(size == argc);

  • Here be arguments:

        int v = strlen(argv[1]) + strlen(argv[2]); // for allocating memroy
    

    We checked argv[1]. However argv[2] may not be there.

        int v = strlen(argv[1]) + strlen(argv[2]); // for allocating memroy
    
        char *str = (char *)malloc(v);
        strcat(str, argv[1]); // putting the string together
        strcat(str, " ");
        strcat(str, argv[2]);
        strcat(str, " ");
    

    v doesn't have enough space to accomodate both strings and two spaces and a terminating 0.

        str = (char *)realloc(str, (v + strlen(argv[i]))); // reallocates enough memory
    

    It doesn't reallocate enough. The subsequent strcat(str, " ") is not accounted for.

🌐
Kotlin Discussions
discuss.kotlinlang.org › native
How to convert from main function args: Array<String> to C type main function argc, char **argv? - Native - Kotlin Discussions
May 4, 2020 - I am trying to use C GUI library with kotlin/native (win 10 pro / x64). Got some progress, but main initialization function of that toolkit library expect parameters exactly the same as one defined for regular C application main function i.e.: (int argc, char **argv).
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_command_line_arguments.htm
Command Line Arguments in C
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.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › C_commandlineargs.php
C command line args
Each command line argument is passed to main in the second parameter, argv, as a string (argv is an array of strings):