Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String Arg is read incorrectly in c

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

./a.out $6$AS$9IwGTn5WbHSalUs4ba3JbOfOUX/v1yD71Z4M2F6Yusz5k2WQEOFxqLIY80tudGtcFttqr/Zq6RIPjHkl/t2Pp1

When running the following program the characters $6$AS$9 are cut from the input using args. Why is this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Problem

This isn’t anything to do with your program and how it treats arguments.

Your shell is treating things that start with a $ as shell variables, which are undefined and so expand to nothing. After $9 it treats the rest as text.

Solution

Enclose the argument in single quotes to prevent the shell from expanding variables: '$6$A$9....'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading