How can GCC know argc if we did not specify it?
Is there a similar mechanism to how GCC determine the number of elements of an array?
Asked
Active
Viewed 23 times
0

Dũng Đào Xuân
- 31
- 1
- 8
-
1The C language mandates that (supporting) operating systems pass `argc` and `argv`, and what they mean. That's it. This is akin to asking 'how does GCC know what `if` means'. It does because the language stipulates the meaning and the compiler implements/follows the language. – underscore_d Dec 23 '20 at 14:50
-
Also, gcc doesn't determine it and **can't** determine it. This can only be done at runtime, and is impossible to do statically. – costaparas Dec 23 '20 at 14:54
-
@underscore_d The answer didn’t satisfy me. – Dũng Đào Xuân Dec 23 '20 at 15:01
-
Explain why then... What specifically else do you want to know? – underscore_d Dec 23 '20 at 15:02
-
@costaparas Do you know why it is implemented that way? Why are we not allowed to specify argc statically? – Dũng Đào Xuân Dec 23 '20 at 15:03
-
2You are aware that `argc` is the number of arguments? In what way would you want it to be specified statically? It depends on *user* input. The compiler doesn't know how many arguments your program will be called with. – costaparas Dec 23 '20 at 15:05
-
@underscore_d I want to know if I invoke my program by ./name argv1 argv2, I did not specify the value of argc then how can the program know it was 3. – Dũng Đào Xuân Dec 23 '20 at 15:08
-
@DũngĐàoXuân Because, again, the C Standard dictates that your operating system must tell the program that via those arguments. So, when the compiled C program is run, the shell puts data on the stack or in registers (tbh I dunno which, but that's a detail that doesn't actually matter for a C coder) in such a way that the C program receives that data as those args. The program knows that because the language says it must. It's kind of tautological as both a question and an answer. – underscore_d Dec 23 '20 at 15:10
-
@costaparas Oh! That clarify a lot. But my main question wasn’t answered yet. – Dũng Đào Xuân Dec 23 '20 at 15:11
-
@underscore_d Ok. Look like it relate much to operating system. I just asked the question because I was curious. Thank you for explaining! – Dũng Đào Xuân Dec 23 '20 at 15:19
-
@costaparas Thank you! – Dũng Đào Xuân Dec 23 '20 at 15:19