What is argc, char * argv[] in C

Introduction

In C, all functions in an executable file are called from a function. main().

Function main() must also be called. For example, from bash .

The call can be both without arguments and with arguments.

Let's start with the first option.

Call without arguments



If you are not going to use command line arguments, you can write. int main()

What exactly does that mean?

Let’s take a look at the simple program we wrote in the file. hello_world.c

#include <stdio.h> int main(int argc, char* argv[]) { printf("Hello, World!\n"); return 0; }

Suppose we compiled our hello_world.c into an object file hello_world with the command

gcc -o hello_world hello_world.c

And now let's run it.

./hello_world

It doesn't matter whether you write

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

or

just int main()

At what point will it become important ?

At the moment when you need to call with arguments

Call with arguments

argc Argument count is the number of command line arguments.

Type argc - int

The minimum value of argc is 1. argv[0] is the name of the program file (in this case, the program file is hello_world.c).

C is a very old language that was created long before it came into being. Modern graphical interfaces.

char* is a type of returned value

Argument Vector - Argument Vector

Consider another program written in the example.c file.

It will expect two arguments of type int .

In the code they will be called a1 and a2. Pay attention to the seventh and eighth lines.

#include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { int a1 = atoi(argv[1]); int a2 = atoi(argv[2]); printf("File name = %s\n",argv[0]); printf(" %d %d \n", a1, a2); return 0; }

Compile this code with the command

gcc -o example example.c

It remains to call the program and pass arguments when calling right in the command line.

./example 7 9

File name = ./example
7 9

We call. example And we pass two arguments right on the command line.

First, we made sure that argv[0] was indeed a file name. Then we saw the passed arguments.

Now, if you want to remove int argc, char* argv[] from main

int main()

The compiler will not understand you and will complain about undeclared argv

example.c: In function ‘main’: example.c:7:16: error: ‘argv’ undeclared (first use in this function) int a0=atoi(argv[0]); ^~~~ example.c:7:16: note: each undeclared identifier is reported only once for each function it appears in

If you remove all references to command line arguments from the code and try running a program with arguments - They will be ignored.

#include <stdio.h> #include <stdlib.h> int main() { return 0; }

gcc -o example example.c
./example 7 9

In other languages

In Python similar function sys.argv

In Bash it default .

Article author: Andrei Olegovich

Related articles
C development
Textbook on C
Boolean in C
Sockets in C
K&R
What is argc, char * argv[]
Function scanf()
Pointers
Structures in C
Operator «arrow» pointer to member structure

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: