Home > Blockchain >  Does ';' somehow terminates a string that you pass as a command line argument?
Does ';' somehow terminates a string that you pass as a command line argument?

Time:11-18

I'm making a program that gets a text input from the command line.

Basically it takes every command line argument as a string , and every string represents a word within the "text" , and I want to print the "text" that gets passed from the command line (without the name of my output) to the screen.

But if one of my command line arguments is a word-string such as 'Hello;' or 'And)' , with a ';' or an ')' at the end of it -as you can see- then it doesn't print the ';' or the ')' etc.

And the way I see it is that , it doesn't even recognize these symbols as a part of the string that gets passed in.

I even tried to print the ';' symbol in a simple 'Hello;' example just to see what is it going to do (Specifically I wrote some code to print only the last character of each string argument).

#include<stdio.h>
#include<string.h>

int main(int argc , char *argv[]){
int i;
    for(i=1 ; i < argc ; i  ){
        char p = *(argv[i]   strlen(argv[i]) - 1);
         printf("%c " , p);
    }
printf("\n");
return 0;
}

When i run the code from command line like this:

./a.out Hello;
It prints
o
Instead of
;

I've tried to print ' ; ' by itself, it just prints nothing and i get an error for the very next string after these ' ; ' , ' ) ' symbols such as error command not found.

If anyone knows why this is happening , or how can I make this program print even ';' symbols etc. please help me , I'm new to C and to programming in gereral.

CodePudding user response:

The ; character is interpreted specially by the shell. It is used to separate multiple commands on the same line.

So you're either need to quote the string:

./a.out "Hello;"

Or escape the ;:

./a.out Hello\;
  • Related