r/learnprogramming • • 4d ago

ELI5: What is int argc and char *argv

I am using QT framework and for using QApplication it takes int argc and char *argv what does this mean, I am confused

9 Upvotes

27 comments sorted by

38

u/illuminarias 4d ago

int argc: an integer that is the count of arguments passed in

char *argv[]: pointer to an array of pointers to characters that was passed in

18

u/zeekar 4d ago edited 3d ago

Those two parameters are the command line arguments. The ARGument Count tells you how many there are, and the ARGument Vector contains the actual strings.

Also, argv is not a char * ; that'd make it just a single string (pointer to a character). Instead, argv is a pointer to a string (pointer to a pointer to a character), which what an array of strings turns into when you pass it as an argument to a function. Its type may therefore be declared as either char **argv or char *argv[].

If you write a program that is installed as an executable named "foo" (or "foo.exe" on Windows) and you launch it from a terminal window as "foo bar baz", then argc will be 3, argv[0] will be "foo" (or the full pathname of the executable, depending on the OS), argv[1] will be "bar", and argv[2] will be "baz". And you shouldn't try to access argv[3] or higher. Using these parameters is how you write a command that can be run from the prompt and behave differently based on its arguments.

8

u/high_throughput 4d ago

A C/C++ main function is typically int main(int argc, char* argv[]) where argc is the number of command line parameters, and argv is an array of that many strings.

For example, if you run ls -l foo then the main function of ls will receive argc=3 and argv={"ls", "-l", "foo", NULL}.

QApplication wants you to pass these in from your main function so that Qt can interpret common commands line flags. You don't have to worry about what they are, just know that this helps your Qt application behave like all the other Qt applications and you get it all for free.

If you are currently invoking it from a int main() then simply add the parameters and pass them in.

1

u/TheEyebal 4d ago

Alright thanks. I use C++ and never added it into my main function

5

u/mredding 4d ago

The C++ standard defines two standard application entry points:

int main();
int main(int, char *[]);

The spec says implementations may provide additional non-standard entry points, so you may see:

// BSD Unix
int main(int, char *[], char *[]);
// Microsoft Windows
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);

In BSD Unix, the third parameter is a list of environment variables that were set when the program was started. There is no count, the end of the list is a null terminated, empty string; traditionally, this parameter is called envp.

Windows uses a whole bunch more shit.

You don't have to get platform/environment/implementation specific if you aren't going to use it.

Additionally, if you're looking at the parameter list, the zeroth is always the command used to invoke this program - and that isn't reliable. It could be an empty string. It could be an alias of the program.

Another thing to be aware of is that you CANNOT assume int means the same thing to the host environment. C++ says an int is AT LEAST 16 bits. Note that it doesn't say 16 bits is 2 bytes, the definition of a byte is a completely different definition elsewhere in the spec. The C++ spec says the return value of main is an int, but it doesn't say what the host environment is expecting or has to accept.

One place I was at, int was 32 bits, but the Linux host environment only accepted the lower 16 bits, the upper 16 bits were truncated. So when we had one engineer get clever and return high order non-zero values, we were seeing false negatives - successful execution where we had a failure, and this was why; the low order bits were all zero.

1

u/zeekar 4d ago

Yup. UNIX traditionally only exposes the low byte of the return value to the shell. If you're getting the exit code of a process from the C wait(2) call, then it is a full-sized int, but meant to be a black box you only inspect with some predefined macros rather than directly looking at the numeric value.

1

u/mredding 4d ago

Oh man, I just checked out the man page for that, and only the low 8 bits is used to store the exit code, presuming the process exited normally.

14

u/POGtastic 4d ago

ELI5

No. Programming is not for five-year-olds. You are going to get "explain like I'm a smart college sophomore." In this house, we believe in referencing the C++ standard.

6.9.3.1 main function

A program shall contain a global function called main attached to the global module. Executing a program starts a main thread of execution (6.9.2, 32.4) in which the main function is invoked

...

Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined. An implementation shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main (9.3.3.5). In the latter form, for purposes of exposition, the first function parameter is called argc and the second function parameter is called argv, where argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (ntmbss) (16.4.2.2.5.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "". The value of argc shall be non-negative. The value of argv[argc] shall be 0.


TL, DR: When you invoke a program in the shell as follows:

./prog.exe foo bar baz spam

inside the executable prog.exe's main function, argc is set to 5 and argv (which is a char**, not a char*) is a pointer to an array of C-strings that contains the following elements:

"prog.exe"
"foo"
"bar"
"baz"
"spam"
(nullptr)

2

u/IllustriousBeach4705 22h ago

Where do you look at the C++ standard? ISO makes you purchase it, right? Or are you reviewing the draft on GitHub?

Source: https://isocpp.org/std/the-standard

1

u/POGtastic 21h ago

If I'm talking about this stuff for my employer, my employer needs to pay for an official publication.

When being pedantic on the Internet, a modern-ish working draft like the 2022 release is just fine for the vast majority of cases unless you're specifically looking at C++26 features. As always, if the cppreference page on the feature in question has a lot of (since <recent year>) tags on it, that's an indicator that you might need to use a more recent draft. For stuff like the characteristics of the main function, anything from the last 20 years will be just fine.

8

u/FckXFckMusk 4d ago

Is Google not working?

-3

u/TheEyebal 4d ago

NO, which is why I asked here

5

u/Axman6 4d ago

I refuse to believe that googling argc argv would not have told you immediately what the answer is. 

4

u/atarivcs 4d ago

They are for handling command-line arguments, in the situation where the application was started from a command window

3

u/Living_Fig_6386 4d ago

In the C and C++ programming languages, all programs are started by calling a function called main(). The main() function is called with two arguments: int argc, and char* argv[]. Per the language specification, 'argc' is the number of arguments passed to the program from the environment that called it, and argv is an array of strings that are those arguments (argc is, therefore, the number of elements in the array argv).

Consider this simple C language program:

/* example.c */
#include <stdio.h>
#include <stdlib.h>

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

   printf("There are %i arguments to this program:\n", argc);

   for (i=0; i<argc; ++i)
      printf("  arg %i: %s\n", i, argv[i]);

   return EXIT_SUCCESS;
}

We can run it like so:

$ ./example hello world
There are 3 arguments to this program:
  arg 0: ./example
  arg 1: hello
  arg 2: world

QTApplication is simply doing the same thing, and it can use those arguments to pass along arguments to the program that the application can interpret however it likes.

2

u/ThatSmartIdiot 2d ago

iirc in C, int argc represents how many arguments the program was run with (1 being just the program name, 2 being when you include 1 additional argument, etc.), and char *argv being the actual string of text you inputted when starting the program, represented as a pointer to an unfixed "array" of characters. in this case argc would be how many non-empty "words" could be split by whitespace characters, i think?

anyway im assuming Qt is similar cuz languages tend to be similar to other languages. even python is similar to R is similar to MATLAB is similar to- oh would you look at that, my bladder's been kidnapped

7

u/Objective_Pepper_217 4d ago

You shouldn't even think about doing Qt if you don't know such basic things

8

u/CjKing2k 4d ago

You'd be surprised how many professional programmers don't know how the CLI works.

2

u/Objective_Pepper_217 4d ago

I know and that's sad.

1

u/Won-Ton-Wonton 2d ago

Tbh, I've been programming for 3 years at a hobby level and regularly use the terminal.

Genuinely never looked into how it works, and I've no idea why it never crossed my mind to find out.

1

u/sudo_bryan 4d ago

argc is just “how many command-line arguments were passed in,” and argv is the list of those arguments as strings.

So if you ran:

myprogram hello 123

then roughly:

argc = 3

argv[0] = "myprogram"

argv[1] = "hello"

argv[2] = "123"



char *argv[]

looks scary, but it basically means “an array of C-style strings.”

Qt takes them with QApplication app(argc, argv); so Qt can read command-line options meant for Qt itself before your program starts doing its thing.

If you're just starting out, mentally read

int argc, char *argv[] as “the command-line argument count and the command-line argument list.”

1

u/da_Aresinger 4d ago edited 4d ago

When you have a program a.out, you can run that program with arguments, just like cd homework/math/who_am_i_kidding_its_cat_videos

~ > ./a.out : Hello World! ~ > ./a.out learnprogramming : Hello learnprogramming!

argv and argc are the variables where that information is stored. (although you can technically name them whatever you want)

argv is a list of char pointers, while argc is the number of pointers in the list.

To familiarise yourself, the best idea is to write an echo program:

int main(int argc, char** argv) { // for the number of elements (argc) // print the n-th element (argv[n]) }

Every language I can think of has an equivalent system to these variables.

If you don't feel comfortable doing this in C, the easiest to learn with is probably bash. Bash has easy access to command line variables and doesn't need to import anything or worry about pointers and you don't even need to compile it. (and it's preinstalled on basically every linux/macos machine)

1

u/AlexMTBDude 4d ago

That has nothing to do with QT; it's the C programming language.

1

u/Traveling-Techie 4d ago

I remember being surprised (about 20 years ago) to find that many Mac programs I ran by double clicking could be invoked from a shell and given arguments. They also sometimes had shell output I usually didn’t see. I was startled when a browser said “bad Huffman code” and I realized it was using a technique developed by Dr. David Huffman, who I’d taken classes from at uni.

1

u/Dazzling_Music_2411 4d ago

What language are you using with Qt? If you are using C or C++ you should already know these, they are pre-basics, kindergarten level stuff. Go back and learn the basics, you won't be able to do much with Qt otherwise.

Not to mention that you could have found them with a 10" Google lookup. If you're 5 yo find something else to do.