There is an abnormality in the value of argc when ‘*’ is passed as one of the arguments calling a program in c.
I made a simple code in c and saved it as ‘test2.c’.Here is the following code of ‘test2.c’—
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
printf("%d\n",argc);return 0;
}
I compiled it and call it as–
dev@ubuntu:~$ gcc test2.c -o t
dev@ubuntu:~$ ./t *
31
So, i am getting the argument count value as 31; whereas if ‘*’ is replaced by any other binary operator ; the value of argc is 2(which is also logically correct).
dev@ubuntu:~$ ./t +
2
I am not able to fathom why is it so….and there is one more interesting thing.when ‘-‘ is used in place of ‘‘;the answer is 2(which is again logically correct)
dev@ubuntu:~$ ./t -*
2
Can anyone help me in this;thanks in advance.
Its just the shell expansion. The shell will expand
*to the filenames (directories included) in the current directory in which the program is executing.+or;does not have special interpretations therefore they are considered as normal strings, but*has special interpretation, thus it is expanded to the list of all files in current directory, which is passed to your program.Try using
./t '*'or./t \*This stops special interpretation of
*. The first one used the bash (looks likeyou are in bash) single quote, which does not make any special interpretation inside the patterns in the quote, and the next one uses escape sequence.