Thread: Using apostrophe as a command line argument

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    4

    Using apostrophe as a command line argument

    Hi all

    I am learning c and I did an exercise in a book. It doesn't seem to behave how expected if one of the command line arguments is an apostrophe.

    Can someone help me?

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int i = 0;
    
      // go through each string in argv 
      for (i=1;i<argc;i++){
        printf("arg %d: %s\n", i, argv[i]);
      }
      
      return 0;
    }
    Here are two different executions of the program (ex13). One with an apostrophe in the command line, another without. Can someone please tell me what is happening here?

    With two words:
    david@Parmenio:~/programming/c$ ./ex13 hello there
    arg 1: hello
    arg 2: there

    With an apostrophe:
    david@Parmenio:~/programming/c$ ./ex13 hello '
    >

    any ideas?
    Dave.

  2. #2
    Registered User
    Join Date
    Jul 2016
    Posts
    1
    You'd have to escape your apostrophe. For example ./ex13 hello \' would result in
    arg 1: hello
    arg 2: '

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    4
    Ahh, i tried that, but now I realise what I think I did wrong. I used a forward slash instead of a backslash.
    'Yep, just tried it. Thanks for your help seanrdev
    .
    Dave.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line argument
    By csit in forum C Programming
    Replies: 4
    Last Post: 09-19-2007, 11:35 AM
  2. Command line argument
    By vaibhav in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2005, 10:38 AM
  3. command line argument help
    By bobnet in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2003, 12:22 AM
  4. command argument line
    By Tonyukuk in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2003, 09:10 AM
  5. Command Line argument
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-12-2001, 03:24 AM

Tags for this Thread