Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard Input C: Incorrect string if $ is present

Tags:

c

gcc

I am trying to take input from the STDIN. If the input string starts with character '$' then the input is not converted to string as it is.

int main(int argc, char*argv[]){
     printf("%s\n",argv[1]);
}

Can someone please let me know to why C compiler replaces the characters with '0' if it encounters the '$' symbol ?

like image 961
Srinivasan Rajappa Avatar asked Jun 26 '15 21:06

Srinivasan Rajappa


1 Answers

It's not your program. It's your shell interpreting it as a variable, then passing its value to your program.

To work around this, escape the $ when you invoke the program from your shell.

./yourprogram '$arg'
like image 64
Mike Andrews Avatar answered Nov 13 '22 21:11

Mike Andrews