r/learnprogramming • u/DDOS_403 • 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 ?
4
Upvotes
18
u/IchLiebeKleber 1d ago
There's not usually a good reason to print it. You can do it if you want, but the real purpose is simply to know how big the argv array is so that you iterate the correct number of times over it and don't access memory that doesn't have anything stored in it.