Im beginner who encounters concept of command line arguments for first time and it really confuses me.
When we write at command line after the programs name anything it gets converted to a string.
But how is main able to do that? Does it interact with system itself and tells system (instructs system itself) everything written here in command line is now argument to my program? Can it do that because system itself is written in c too so it can interact with it outside of program it itself is part of?
Also is there equivalent of that in gui?
And i understand it can take ints and convert them to strings. But what if we wanted to type in other data type as argument in command line.Like boolean for example, is it error like data provided not matching argument required by function?
CodePudding user response:
The system passes everything as a string, because it cannot know what kind of datatypes your program can handle or how it stores them. It does not know that your program is written in C. For all it knows, it could be Python, Java, JavaScript, Rust etc. and those all have and store different kinds of datatypes slightly differently, so the system cannot assume anything. Therefore programs just receive everything as a string and leave it to the programmer to convert it to the right datatypes if they so wish.
CodePudding user response:
In typical implementations, command-line arguments are parsed by the command-line shell and then passed to the program being executed. In Unix systems, a new program is executed by calling a system routine in the “exec” family, and the arguments are passed to those routines as a list of pointers to character strings (in one form or another).
In any hosted C implementation that conforms to the C standard, the program arguments are prepared before main
is called. main
does not interact with the system to do that; it is done before main
is called.
But what if we wanted to type in other data type as argument in command line.
You cannot type any other data type. On your keyboard, you can only press keys. There is no key on your keyboard that creates a boolean value or even an int
value like 3. When you press a key1, a signal is sent to the computer, with information about which key you pressed. Software on the computer interprets the pressed key. Roughly, in a “normal” text-input terminal mode, if the pressed key represents some character, like “A” or “3” or “ ”, that character will be added to a buffer that is being used to collect user input. When the user presses Return/Enter or does certain other actions, that buffer of characters will be sent to the current program attached to the terminal. (There may be other terminal modes available in which keypresses are processed differently, such as a sent directly to a program routine, which might interpret them as commands in a game being played.)
Footnote
1 The specifics of this may depend on the key and the hardware. Pressing a Shift key might not send a signal to the computer; the keyboard might merely use that to affect the signals it sends when you press another key on conjunction with Shift.