By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

How 'main(int argc, char ** argv)' is implemented?

P: n/a
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?

My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
.......(omitted)

char input[256];
char * token;
printf("/");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you

Mar 19 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11*********************@o5g2000hsb.googlegroups.c om>,
william <wi**********@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
The have nothing to do with stdin. Typically they are arguments
passed on the command line and broken up by some part of the operating
system - in unix, it's the shell.
>And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No, these both mean the same thing when used to declare the arguments
to functions.
>My initial purpose was to parse the input from the keyboard like
[...]

It's unlikely that the mechanism used for splitting command line
arguments will help you with this.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 19 '07 #2

P: n/a
On 19 Mar, 16:17, "william" <william.m...@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)

char input[256];
char * token;
printf("/");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...

If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Mar 19 '07 #3

P: n/a
On Mar 19, 12:29 pm, mark_blue...@pobox.com wrote:
On 19 Mar, 16:17, "william" <william.m...@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?

That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?

No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you

What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...

If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Thank you for the replies!

Mar 19 '07 #4

P: n/a
On Mar 19, 12:42 pm, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:29 pm, mark_blue...@pobox.com wrote:
On 19 Mar, 16:17, "william" <william.m...@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.

Thank you for the replies!
I found a solution:

using fread:
....
fread(destination_array, 1, sizeOfArray, stdin);
....
Then I process this array, and everytime I got a whitespace=20(ascii
value), I append '\0' at the end and use it as an argument.

Is there any better way to do the same thing? Thank you

Mar 19 '07 #5

P: n/a
On Mar 19, 12:52 pm, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:42 pm, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:29 pm, mark_blue...@pobox.com wrote:
On 19 Mar, 16:17, "william" <william.m...@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Thank you for the replies!

I found a solution:

using fread:
...
fread(destination_array, 1, sizeOfArray, stdin);
No, here it still doesn't work. because I have to input exactly
'sizeOfArray' characters to finish this call, which is not the case I
want to simulate.
...
Then I process this array, and everytime I got a whitespace=20(ascii
value), I append '\0' at the end and use it as an argument.

Is there any better way to do the same thing? Thank you

Mar 19 '07 #6

P: n/a
On 19 Mar, 17:04, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:52 pm, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:42 pm, "william" <william.m...@gmail.comwrote:
On Mar 19, 12:29 pm, mark_blue...@pobox.com wrote:
On 19 Mar, 16:17, "william" <william.m...@gmail.comwrote:
I wonder how 'main(int argc, char ** argv)' is implemented? How
does it get
the string literals separated by whitespace from the stdin stream?
That's a job for the host environment, for example the unix shell. You
could look at some shell source, but whether it would help is
questionable...
And is there any difference between 'char**' and 'char *[ ]'(the
second argument of function main)?
No.
My initial purpose was to parse the input from the keyboard like
'main' does, code is here:
#include <stdio.h>
#include <string.h>
......(omitted)
char input[256];
char * token;
printf("/");
scanf("%s", input);
token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)
****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.
Can anyone tell me how I can implement this: get and parse the user
input conveniently? Thank you
What do you believe scanf("%s",input) will do with the data on the
standard input stream?
[Hint: it's generally not what you want...] This is probably in the
FAQs somewhere...
If you just want to read a line from stdin, why not use a sensible
function, like fgets() or one of the alternatives (e.g. ggets()) which
are posted here from time to time.
Thank you for the replies!
I found a solution:
using fread:
...
fread(destination_array, 1, sizeOfArray, stdin);

No, here it still doesn't work. because I have to input exactly
'sizeOfArray' characters to finish this call, which is not the case I
want to simulate.
If you want to read a line of input (and I think you probably do),
then fgets() is the best bet, if you only want to use standard library
routines.
...
Then I process this array, and everytime I got a whitespace=20(ascii
value), I append '\0' at the end and use it as an argument.
Is there any better way to do the same thing? Thank you
Once you've got your string, you could use strtok() (with care).

Mar 19 '07 #7

P: n/a
william wrote:

<snip>
My initial purpose was to parse the input from the keyboard
<snip>
#include <stdio.h>
#include <string.h>
......(omitted)

char input[256];
char * token;
printf("/");
scanf("%s", input);

token=strtok(input, ' '); //use whitespace as delimiter;
^using this line, I intent to parse the input from keyboard into
different tokens(which will be arguments for my further use)

****************
However, the problem was, I found 'scanf' can only get the first
string ending with '\0'. For example, if I type "command arg1 arg2
arg3" following the hint "/" that I printed, scanf can only get
command into the array 'input', and all arg1, arg2... are truncated.
And thus my purpose can not be reached by token=strtok(input, ' ')
repeatedly, because input has only 'command\0'.

Can anyone tell me how I can implement this: get and parse the user
input conveniently?
try fgets() it reads whole lines.

--
Nick Keighley

Mar 20 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.