Hi All,
I am not able to understand the behaviour of the following program
#include<stdio.h>
int main(int agrc,char *argv[])
{
printf("\nReceived val =%s\n",argv[1]);
return 0;
}
When I run the program as follows. I get the correct output
./test '*'
Received val =*
But when I run the program as below. with out '' .
./test *
Received val =1.c
I am not able to understand this difference. Is it happening because
of undefined behaviour? or it is because of the behaviour of the bash
shell?
|
|
0
|
|
|
|
Reply
|
somenathpal (141)
|
5/3/2012 4:11:52 AM |
|
On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
<somenathpal@gmail.com> wrote:
>Hi All,
>
>I am not able to understand the behaviour of the following program
>
>#include<stdio.h>
>int main(int agrc,char *argv[])
>{
> printf("\nReceived val =%s\n",argv[1]);
>
> return 0;
>}
>
>When I run the program as follows. I get the correct output
> ./test '*'
>
>Received val =*
>But when I run the program as below. with out '' .
>
> ./test *
>
>Received val =1.c
>I am not able to understand this difference. Is it happening because
>of undefined behaviour? or it is because of the behaviour of the bash
>shell?
In your shell, is * a wild card? If so, what does the shell replace
it with?
--
Remove del for email
|
|
0
|
|
|
|
Reply
|
schwarzb3978 (1358)
|
5/3/2012 4:58:01 AM
|
|
On May 3, 9:58=A0am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
>
>
>
>
>
>
>
>
>
> <somenath...@gmail.com> wrote:
> >Hi All,
>
> >I am not able to understand the behaviour of the following program
>
> >#include<stdio.h>
> >int main(int agrc,char *argv[])
> >{
> > =A0 =A0printf("\nReceived val =3D%s\n",argv[1]);
>
> > =A0 =A0return 0;
> >}
>
> >When I run the program as follows. I get the correct output
> > ./test '*'
>
> >Received val =3D*
> >But when I run the program as below. with out '' .
>
> > ./test *
>
> >Received val =3D1.c
> >I am not able to understand this difference. Is it happening because
> >of undefined behaviour? or it is because of the behaviour of the bash
> >shell?
>
> In your shell, is * a wild card? =A0If so, what does the shell replace
> it with?
Yes * is a wild card in my shell. I did not get your second
question . My understanding is wildcard is used with some regular
expression and in that contest it means all occurrences.
|
|
0
|
|
|
|
Reply
|
somenathpal (141)
|
5/3/2012 8:12:36 AM
|
|
somenath <somenathpal@gmail.com> wrote:
> On May 3, 9:58 am, Barry Schwarz <schwa...@dqel.com> wrote:
> > On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
> > <somenath...@gmail.com> wrote:
> > >I am not able to understand the behaviour of the following program
> >
> > >#include<stdio.h>
> > >int main(int agrc,char *argv[])
> > >{
> > > printf("\nReceived val =%s\n",argv[1]);
> >
> > > return 0;
> > >}
This will result in undefined behaviour if the program is
started without any arguments since in that case argv[1]
will be a NULL pointer and that's something printf() isn't
required to accept for the '%s' specifier. Otherwise it will
print whatever it receives as the first argument when the
program is uinvoked.
> > >When I run the program as follows. I get the correct output
> > > ./test '*'
> >
> > >Received val =*
> > >But when I run the program as below. with out '' .
> >
> > > ./test *
> >
> > >Received val =1.c
> > >I am not able to understand this difference. Is it happening because
> > >of undefined behaviour? or it is because of the behaviour of the bash
> > >shell?
> >
> > In your shell, is * a wild card? If so, what does the shell replace
> > it with?
>
> Yes * is a wild card in my shell. I did not get your second
> question . My understanding is wildcard is used with some regular
> expression and in that contest it means all occurrences.
All this isn't really abount C - it's about how the shell
you start your program from interprets a * character.
Many shells (including bash) leave a * when enclosed by
single quotes unmodified (except removing the single
quotes, the single quotes themselves being "meta-charac-
ters, telling the shell "Leave everything enclosed by
them alone") but if it appears "naked" they replace it
with a list of all files in the current directory. If
you try the command "echo *" you may find that this will
result in the shell outputting a list of the names of
all files in the current directory. And in that case,
if you use a "naked" * after the name of your program,
the shell will "expand" the * into such a list of file
names, chop it up at space characters and pass the re-
sulting set of strings to your program in a way that
they end up in the 'argv' argument.
So all this isn't anything related to C but is all about
how the shell deals with a * (and other characters that
have a special meaning in the shell). Your C program only
"sees" the results of the transformations the shell applies.
So the place to look for information is the documentation
of your shell (or maybe ask in e.g. comp.unix.shell),
whatever you do in C here does not have any influence on
that.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
0
|
|
|
|
Reply
|
jt68 (1134)
|
5/3/2012 9:06:43 AM
|
|
On May 3, 9:12=A0am, somenath <somenath...@gmail.com> wrote:
> On May 3, 9:58=A0am, Barry Schwarz <schwa...@dqel.com> wrote:
>
>
>
>
>
>
>
> > On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
>
> > <somenath...@gmail.com> wrote:
> > >Hi All,
>
> > >I am not able to understand the behaviour of the following program
>
> > >#include<stdio.h>
> > >int main(int agrc,char *argv[])
> > >{
> > > =A0 =A0printf("\nReceived val =3D%s\n",argv[1]);
>
> > > =A0 =A0return 0;
> > >}
>
> > >When I run the program as follows. I get the correct output
> > > ./test '*'
>
> > >Received val =3D*
> > >But when I run the program as below. with out '' .
>
> > > ./test *
>
> > >Received val =3D1.c
> > >I am not able to understand this difference. Is it happening because
> > >of undefined behaviour? or it is because of the behaviour of the bash
> > >shell?
>
> > In your shell, is * a wild card? =A0If so, what does the shell replace
> > it with?
>
> Yes * is a wild card in my shell. =A0I did not get your second
> question . My understanding is wildcard is used with some regular
> expression and in that contest it means all occurrences.
In DOS shells (command.com, cmd.exe etc), your program is passed the
wildcard, and is expected to expand that wildcard and interpret the
results. In Unix shells, the shell expands the wildcard itself (unless
it was quoted) before the program is called, and that expansion is
passed in argv[].
So the naked * never gets passed to program, the shell replaces it
with the names of the files in your directory - and the first of those
is apparently "1.c"
|
|
0
|
|
|
|
Reply
|
gwowen (518)
|
5/3/2012 1:00:50 PM
|
|
On May 3, 6:00=A0pm, gwowen <gwo...@gmail.com> wrote:
> On May 3, 9:12=A0am, somenath <somenath...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > On May 3, 9:58=A0am, Barry Schwarz <schwa...@dqel.com> wrote:
>
> > > On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
>
> > > <somenath...@gmail.com> wrote:
> > > >Hi All,
>
> > > >I am not able to understand the behaviour of the following program
>
> > > >#include<stdio.h>
> > > >int main(int agrc,char *argv[])
> > > >{
> > > > =A0 =A0printf("\nReceived val =3D%s\n",argv[1]);
>
> > > > =A0 =A0return 0;
> > > >}
>
> > > >When I run the program as follows. I get the correct output
> > > > ./test '*'
>
> > > >Received val =3D*
> > > >But when I run the program as below. with out '' .
>
> > > > ./test *
>
> > > >Received val =3D1.c
> > > >I am not able to understand this difference. Is it happening because
> > > >of undefined behaviour? or it is because of the behaviour of the bas=
h
> > > >shell?
>
> > > In your shell, is * a wild card? =A0If so, what does the shell replac=
e
> > > it with?
>
> > Yes * is a wild card in my shell. =A0I did not get your second
> > question . My understanding is wildcard is used with some regular
> > expression and in that contest it means all occurrences.
>
> In DOS shells (command.com, cmd.exe etc), your program is passed the
> wildcard, and is expected to expand that wildcard and interpret the
> results. In Unix shells, the shell expands the wildcard itself (unless
> it was quoted) before the program is called, and that expansion is
> passed in argv[].
>
> So the naked * never gets passed to program, the shell replaces it
> with the names of the files in your directory - and the first of those
> is apparently "1.c"
Many thanks for all the response. So does the shell not do any
expansion of wild card while giving input to getchar()? I presume no.
As for the following program I get expected output.
#include<stdio.h>
int main(void)
{
int c;
c =3D getchar();
putchar(c);
return 0;
}
$ ./a.exe
*
*
|
|
0
|
|
|
|
Reply
|
somenathpal (141)
|
5/3/2012 1:32:13 PM
|
|
On 05/03/2012 09:32 AM, somenath wrote:
> On May 3, 6:00 pm, gwowen <gwo...@gmail.com> wrote:
....
>> In DOS shells (command.com, cmd.exe etc), your program is passed the
>> wildcard, and is expected to expand that wildcard and interpret the
>> results. In Unix shells, the shell expands the wildcard itself (unless
>> it was quoted) before the program is called, and that expansion is
>> passed in argv[].
>>
>> So the naked * never gets passed to program, the shell replaces it
>> with the names of the files in your directory - and the first of those
>> is apparently "1.c"
>
> Many thanks for all the response. So does the shell not do any
> expansion of wild card while giving input to getchar()? I presume no.
What the shell does is entirely up to the shell. Different shells do
different things. You have to specify a specific shell before that
question can be answered.
The ones that have been described to you limit wild-card expansion to
the command line, if at all. They affect your stdin only insofar as they
allow it to be redirected from the terminal to some other source: a
file, or the output of another program. However, there's no inherent
reason a shell couldn't be created that interposes itself between the
normal stdin and your program, performing wild card expansion. It would
be the equivalent of the following command line in csh:
expand_wildcards | your_program
where expand_wildcards reads stdin, does what it's name suggests, and
writes the result to stdout.
This would serve no useful purpose I can immediately think of, but my
point is that only by checking the documentation for a particular shell
can you learn what it really does.
|
|
0
|
|
|
|
Reply
|
jameskuyper (5137)
|
5/3/2012 2:35:55 PM
|
|
On May 4, 1:32=A0am, somenath <somenath...@gmail.com> wrote:
> Many thanks for all the response. So does =A0the shell not do any
> expansion of wild card while giving input to getchar()? I presume no.
The shell (the program which interprets your typed commands)
does the wildcard expansion before it loads your program.
So your command:
./test *
gets changed by the shell into:
./test 1.c 1.o a.txt blah blah
(where that is a list of all the files in the current
directory) , before it even figures out what "test" is,
let alone gets up to loading your executable into memory.
If you typed:
echo ./test *
you would see what arguments get passed to your program.
|
|
0
|
|
|
|
Reply
|
oldwolf (2278)
|
5/7/2012 5:38:26 AM
|
|
On Thu, 3 May 2012 06:00:50 -0700 (PDT), gwowen <gwowen@gmail.com>
wrote:
<snip: running program with argument * got filename instead>
> In DOS shells (command.com, cmd.exe etc), your program is passed the
> wildcard, and is expected to expand that wildcard and interpret the
> results. In Unix shells, the shell expands the wildcard itself (unless
> it was quoted) before the program is called, and that expansion is
> passed in argv[].
>
> So the naked * never gets passed to program, the shell replaces it
> with the names of the files in your directory - and the first of those
> is apparently "1.c"
Yes but. In DOS and Windows at the process level, the child gets the
whole command line and is responsible both for parsing it and handling
wildcards, whereas in Unix the shell does both -- now; in very early
Unix this was actually done by a separate program called 'glob' and
the feature is still called 'globbing'.
But at the C level, all(?) implementations on DOS/Windows in their
startup code do Unix-style parsing and pass to main as argv[] like
Unix, and some do wildcards in the process -- sometimes controlled by
a compile or link option.
|
|
0
|
|
|
|
Reply
|
dave.thompson2 (767)
|
5/13/2012 4:15:48 AM
|
|
|
8 Replies
25 Views
(page loaded in 0.191 seconds)
|