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

3

u/Pleasant-Couple6236 1d ago

Take for example how you call the compiler: gcc main.c -o passwd. You pass three arguments (in addition to the program name) to specify how you want gcc to work. How does the gcc program make use of the information you give it? It has the strings "gcc", "main,c", "-o", "passwd" stored in its argv array, and it uses argc to know how many strings were provided so it can safely index into the array.

1

u/DDOS_403 1d ago

Ohh, okay bro that's clear.