Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$ followed by a char in command line argument in C

Tags:

c

command-line

I just wrote a program in c language which uses command line arguments and i tried to print the first argument. when i execute program with following command

./a.out $23

and try to print the first argument using the below code

printf("%s", argv[1]);

the output is just

3

Am i missing something here, that command line arguments are treated differently if some special characters are present. can some one explain this behavior.

like image 674
Yashwanth Kumar Avatar asked Dec 20 '25 11:12

Yashwanth Kumar


2 Answers

You need to escape the $ character.

Try this:

./a.out \$23
like image 200
squiguy Avatar answered Dec 23 '25 05:12

squiguy


Presumably the $2 is being treated as a shell variable. Try escaping the dollar sign:

./a.out \$23
like image 22
chrisaycock Avatar answered Dec 23 '25 04:12

chrisaycock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!