2

I am having really strange issue. I am passing multiple arguments in command line of my Objective-C program via the following commands.

gcc -framework Foundation main.m calculator.m -o prog1

./prog1 3 + 5 / 8

When I print argc and argv[] it gives me following output that is correct

argc = 6

argv[] = prog1,3,+,5,/,8

The problem occurs when I insert a "*" in the input: ./prog1 3 + 5 / 8 * 8 Then it gives me really strange output

argc = 12
argv [] = 3,+,5,/,8,calculator.h,calculator.m,main.m,prog1,8

What's going wrong?

jscs
  • 63,694
  • 13
  • 151
  • 195
Adeel Ahmed
  • 67
  • 1
  • 6

2 Answers2

3

The shell that you're running from is interpreting the asterisk as a file glob; it's replacing that character with the names of all the files in the current directory before passing the arguments to your program.

You need to escape the asterisk \* in order for it to be passed along literally. This also applies to other characters that your shell interprets, such as quote marks " and ', backslashes, and ampersands.

jscs
  • 63,694
  • 13
  • 151
  • 195
2

You have 2 options

  • Escape each single special symbol with a backslash (\*) or
  • Double-quote the argument (as in "*").
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43