strange behaviour of the program.

S

somenath

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?
 
B

Barry Schwarz

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?
 
S

somenath

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 '' .
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.
 
J

Jens Thoms Toerring

somenath said:
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.
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
 
G

gwowen

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?

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.

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"
 
S

somenath

On Wed, 2 May 2012 21:11:52 -0700 (PDT), somenath
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?
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.

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 = getchar();
putchar(c);

return 0;
}

$ ./a.exe
*
*
 
J

James Kuyper

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.
 
O

Old Wolf

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.

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.
 
D

David Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top