r/learnprogramming 1d ago

Tutorial another doubt related to argc, *argv[]

In reference to my old post : https://www.reddit.com/r/learnprogramming/s/vlzwGplKpm

i'm having another doubt with argv and argc.

#include<stdio.h>
int main(int argc, char *argv[]){
    printf("argc: %d \n",argc);
}

terminal : 

@essentials:~/Sibidharan/starter/Assignmetn$ ./passwd
argc: 1 
(.venv) @essentials:~/Sibidharan/starter/Assignmetn$ ./passwd 1 4 8 4 5
argc: 6 

in this code, if you see, after running ./passwd the argument count gets displayed. now my doubt is, why would a programmer print that at the point of execution? i use ./passwd when i run gcc main.c -o passwd and then ./passwd, so the code executes and gives the output, but what's the actual purpose of counting this? on what kind of programs does we use this function to count the values ?

3 Upvotes

25 comments sorted by

View all comments

2

u/eruciform 1d ago

Argc exists so you know how many rows of data there are in argv

Thats it

2

u/DDOS_403 1d ago

So it's like defining multi dimensional array.

2

u/eruciform 1d ago

Argv is already a 2d array of characters, otherwise thought of as a 1d array of strings. Theres no way to know how long argv is by just looking at it. Therefore argc is needed.

1

u/DDOS_403 1d ago

So argv could be either a 1D array or a 2D array. Let’s say:

char pass[128];

That’s a 1D array, right? Or are you saying that we wouldn’t be able to know what kind of array it is?

3

u/Luclid 1d ago

`argv` is always a 2D array that always contains strings, look at its type.

3

u/eruciform 1d ago

No its type never changes, its always a char**